duktape
duktape copied to clipboard
Dump complete bytecode (including referenced functions)
Hi,
I am attempting to serialize a function from one heap, and restore it on another heap (within the same application instance), and I've hit an issue where if the serialized function makes a call to another function, it appears that the second function is not included in the bytecode.
Eg with the following code:
function foo(){
print("foo");
}
function foobar(){
foo();
print("bar");
}
If I serialize function foobar
via duk_dump_function
, restore it on another heap via duk_load_function
, I see the error
ReferenceError: identifier 'foo' undefined
Which I guess makes sense, as it only serialized the "call function foo" instruction, rather than the actual implementation of foo.
My question is, is it possible to completely serialize the foobar function from the duktape API, including any referenced functions?
If it isn't possible in a single call, is there a way to introspect the JS function from C and manually call duk_dump_function
on all the other functions referenced?
Thanks
The serialization is currently limited to a single function (or program). Serializing dependent functions would quickly become equivalent to generic serialization of arbitrary heaps because there may be loops, scoped variables, etc. There's unfortunately no mechanism to introspect a function from C at present.
Note that if you want to bytecode dump a source file, you can just compile it as a "program". It will then compile to a single function containing all the functions and top level code in the program. You can serialize that function and then load and execute it to recover all the functions.