I'm new to QuickJS, and I'm trying to make a basic program that loads and runs a script.
Here's the snippet of code that loads and runs the script:
auto jsr = shared_ptr<JSRuntime>(JS_NewRuntime(), JS_FreeRuntime);
for (auto &f : files){
auto ctx = shared_ptr<JSContext>(JS_NewContext(jsr.get()), JS_FreeContext);
js_init_module_os(ctx.get(), "os");
js_init_module_std(ctx.get(), "std");
size_t bufLen = 0;
auto buf = js_load_file(ctx.get(), &bufLen, f.c_str());
cout << "Starting Evaluation\n";
JS_Eval(ctx.get(), (char*)buf, bufLen, f.c_str(), JS_EVAL_TYPE_MODULE);
cout << "Ending Evaluation\n";
}
And here is the script I'm running:
import {sleep} from 'os';
for (let i = 0; i < 100; i++)
{
print("First Sleep: "+i);
sleep(1000);
}
When This executes, I get a segfault Right after "Starting Evaluation", so I know it's happening inside the JS_Eval call.
I can run this script just fine using the qjs utility. Looking at qjs.c, there's quite a bit of additional processing done by qjs compared to my program. However, it's very complex and I don't understand exactly what I'm doing wrong compared to qjs.
Has anyone encountered this kind of issue before?
Thanks