V8 crashing with ‘Uncaught RangeError: Maximum call stack size exceeded’

While working with V8 in another process, it kept crashing at initialization with the message ‘Uncaught RangeError: Maximum call stack size exceeded’. I wasn’t doing anything big, just initialization.
After about 3 hour of debugging, I found out what was wrong. Normally, V8 will assume that you have no more than 512KB of stack space. To prevent stack overflows, it will take a stack address, substract 512kb from it, and remember that address. If it ever passes that address, it’ll throw a RangeError.

The problem lies in the fact that the program I was working with had a stack somewhere around 0x60000 or 384kb. V8 then substracts 512 from that, but instead of getting a nice stack-boundary, it ends up with an incredibly big number due to integer underflow. The next time it checks the stack, it compares the stack address (still somewhere around 0x60000) with it’s calculated stack limited (which, due to the underflow, is about 0xFFFE0000), and assumes it has a stack overflow.

To fix this, I had to manually set the stack limit. Basically, the following code takes a random stack address, divides it by 2, and uses that as the lower-limit to the stack:

	v8::ResourceConstraints rc;
	rc.set_stack_limit((uint32_t *)(((uint32_t)&rc)/2));

Javascript Haskell mash-up

With LiveScratcher I’m working a lot with JavaScript, and I’ve really grown to love the language. For example, today I stumbled upon this blog post. It’s a JavaScript implementation of a lot of Haskell tricks and functions, including a quick “curry” function (read the blog post to find out what it is).

Just felt that was worth sharing 🙂

LiveScratcher: Now what?

LiveScratcher, and it’s early abandoned brother project Modumes, have been in the pipeline for months now. I think I had the initial idea of a modular extension platform for Messenger well over a year ago. Either way, I never really had a lot of time to put some real work into it. Until now, that is. Holidays have started two weeks ago, and I’ve been crashing at my parent’s place (free food 😉 ) and working on LiveScratcher non-stop since. I hadn’t quite expected it (what would you expect from a project that’s been in the making for months without any results), but I feel that the end of the road (or at least something worth showing) is near 🙂

I’ve put up the initial scaffolding for the Messenger API, and am currently filling in the functionality for the Messenger, Contact, and Conversation classes. The DirectUI (messenger’s user-interface library) module is in a somewhat usable state, and I’ve even written an old-style UIFile parser to it (written completely in Javascript). One package I haven’t started on is the Interop (= communication with external libraries) one, but the plan for that one is well established in my head, and shouldn’t be too hard to implement.

For now I’m aiming for something that is on-par with Messenger Plus!’s scripting, but once I reach that I hope to keep expanding and surpass it greatly. One of the advantages I have is the extensibility. Each package can export certain functionality, and as such anyone is free to extend the API as they wish. Someone could make a package solely dedicated to communicating with facebook or twitter, and other developers can then use those packages from their scripts, for example creating a script that keeps your facebook or twitter friends updated on how many contacts you have.

Here’s a list of a few things that are done and worth mentioning:

  • Minimal main system: the main system *only* contains the bare necessities needed to load the modules and scripts. As such you can replace virtually anything with your own implementation; Don’t like the standard Console? take it out and write your own.
  • Google’s V8 JavaScript engine: The same engine that’s behind the blittering speed of Google Chrome. A downside is that things like “new ActiveXObject” aren’t supported, but upsides are plenty: It’s fast, it’s flexible, it’s standards compliant, and it actually allowed me to do the DirectUI stuff (Microsoft’s JScript was a bitch when it came to threading, V8 just sticks to one)
  • Code searching package: No more hard-coding of patch offsets, simply define what kind of pattern you’re looking for, and this package will find the offset(s) for you. It’ll optimize multiple patterns so only one pass over the memory is needed, so it should be really fast, and it’ll run in the background and only keep your script waiting minimally: it will return the offset the millisecond it’s found, and continue searching in the background.
  • Quick and simple console package that outputs script errors, and exports functions to add messages, warnings, or errors so you have an easy way to output data to the user.
  • DirectUI access: Want to remove a button from messenger? add a new one? Move the wordwheel to the bottom of the contact list? It’s all possible.
  • UIFile parser completely implemented in JavaScript, allows you to define your own output callbacks (or use existing ones), so someone could easily stick in a UIB compiler.
  • Packages can consist of modules (DLLs) or scripts, or even a combination of both.
  • The actual Messenger API is a mess, there’s about three different ways to do things, and half of the functionality is often broken. But as a package developer you don’t have to worry about that: I’ve taken the time to write a Messenger package that takes care of all that for you, and exports an easy to use interface for your scripts to use 🙂
  • Need some functionality that’s not yet in LiveScratcher but is in Plus? For now there’s a PlusCom package that allows you to make a (string values only) bridge between a Plus! script and a LiveScratcher package.
    I’m not sure if this will be useful when it’s all done, but it was a nice experiment, and might come in handy with other things.
  • Need to pop up a toast? That’s already in the Messenger module 🙂 with callbacks and everything. And not fake lookalike toasts, nope, actual real Messenger toasts with custom text.

Now, it all sounds really great when I put it like this, but there’s also a bunch of things that are missing (but that I do plan to implement some day):

  • Debugger: as of now, all you have is some errors in the console if something goes wrong. I’m planning on exposing the V8 debugger protocol and write a nice debugger plugin for Eclipse or Notepad++ or something like that.
  • More advanced Interop: For now I’m focussing on an interop approach like Plus!. A simpe way to call external libraries with integers or strings, and some memory block class for pointer stuff. When I have the time I’m hoping to come up with a nicer approach that allows you to specify a structure and it will make a memory-block automatically and catch all get/set calls to it.
  • Protocol level stuff: For now I’m focussing on exposing all the API based stuff, but at some point I hope to write a proxy/protocol-analysing package that would allow for similar functionality to Messenger Discoveries’s plugins.
  • RichEdit hooking: It’s going to be a big project, but I do hope to implement some kind of RichEdit hook and expose that, so scripts can implement new mark-up stuff. For example implement Plus!’s coloring, or implement a LateX package that automatically converts latex to images.

Well, now you know most of what it is and my plans about it. If for some reason you got excited (sure hope you did 😉 ) and want to take a look and give some feedback, please don’t hesitate to contact me. Just leave a comment and I’ll get in touch with you. I’ll gladly send it over and give you all the help you need to get using it. At this point however I’m not quite comfortable with just uploading it here (as it’s still very very crude and rough around the edges), so you will have to personally ask me if you want to take a peek 😉

JavaScript madness

There’s quite a lot of people out there that absolutely hate JavaScript or ECMAScript, mostly because the different DOM/DHTML implementations in browsers are often incompatible. Yet it appears there are also people out there that love the dynamic nature of the language itself, and I think I’m becoming one of them. To me it’s a language that allows you to hack small things together relatively easy (when writing some exercises that were supposed to be written in pseudo code in JS a professor noted that it resembled pseudo-code really closely, and I should recommend it to the lecturer), whereas you’re also able to write bigger projects by leveraging the OO capabilities.
Anyway, enough about that, ever since I started using Google’s V8 engine in LiveScratcher for the scripting I’ve been experimenting with JS, and after having done some Data structures exercises with it, I started getting passionate about implementing some data structures in JavaScript. I’ve currently written a Heap (with a PriorityQueue using it), and have just finished an AVLTree implementation (which I will use for a map), and will probably move on to implement all structures from my book 🙂 I’m not sure if I’ll ever release this into the wild, or if it’s even worth releasing into the wild, but I’m pretty proud of it so far, and it forces me to learn more about the data structures involved, as well as keeps my JS skills up to par.

On a different note: I’ve set up a website for LiveScratcher with a wiki and an installer for an alpha version. Check it out at http://www.livescratcher.com/