So far I've written a bunch of functions to abstract around the JS engine and fit in with the style of the rest of the codebase:https://github.com/fatcerberus/minisphere/blob/chakra-js/src/shared/ecmunch.cI've done some testing with it and I can successfully set object properties, create functions, eval code and get the result back, etc. I haven't figured out how to get it to load a module yet, though.
Something interesting about CC is that any JsValueRefs stored as local variables on the stack are apparently protected from garbage collection without needing to explicitly root them nor call JsRelease(). I'm not really sure how that works: Does the GC actually examine the native C/C++stack frames looking for refs?Although I guess you'd need to have a system like that if you're doing GC off-thread. Otherwise the object might get collected before you had a chance to root it.
A Chakra runtime will automatically track JsRef references as long as they are stored in local variables or in parameters (i.e. on the stack). Storing a JsRef somewhere other than on the stack requires calling JsAddRef and JsRelease to manage the lifetime of the object, otherwise the garbage collector may free the object while it is still in use.
A couple of notes on my points above:V8 documentation: a lot of content on their wiki has been written this year, so it may have been a lot worse before.
static js_value_t*js_chakraTest(js_value_t* this, int num_args, js_value_t* args[], bool is_ctor){ printf("Something stupid happened and then you got eaten by a %s!\n", js_value_as_string(args[0])); return NULL;}js_value_t* function = js_value_new_function("chakraTest", js_chakraTest, 0);js_value_set(js_global_object(), "chakraTest", function);js_value_unref(function);js_value_unref(js_value_new_eval(lstr_new("chakraTest('pig');")));
Something stupid happened and then you got eaten by a pig!
The js_value_unref()s are an unfortunate side effect of the abstraction--I'm not longer passing around the JsRefs directly, so I need to manage the object lifetime manually.
Quote from: Fat Cerberus link=msg=9964The js_value_unref()s are an unfortunate side effect of the abstraction--I'm not longer passing around the JsRefs directly, so I need to manage the object lifetime manually. Hmm, will that cause a problem with garbage collection? I can see it getting annoying at least, anyway you could add at least some of them to the abstraction layer?I assumed you'd have got a function callback set up sooner I guess you focussed on the abstraction layer first?Good work nonetheless.(I wrote an equivalent test for jsc on Saturday but without any abstraction)
Regarding the abstraction layer: I wanted to get it out of the way first, to avoid making the same mistake I made with Duktape where there was no abstraction to begin with and implementing the extra layer now would be prohibitively time-consuming relative to the benefits. It's bad enough I'm going to have to refactor everything in pegasus.c and vanilla.c (and probably some stuff in map_engine.c too). The v1 and v2 APIs taken together account for about 500-600 functions, methods and properties, the only saving grace is going to be that those are mostly all stubs with the actual work done deeper in the engine.
If you ever get miniSphere to work on Android, let me know! Allegro is supposed to support it but I couldn't figure out their build systems.