Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simpler way to call js_init_module_os() #57

Closed
drschwabe opened this issue Jul 8, 2022 · 2 comments
Closed

Simpler way to call js_init_module_os() #57

drschwabe opened this issue Jul 8, 2022 · 2 comments

Comments

@drschwabe
Copy link

drschwabe commented Jul 8, 2022

This test calls the quickjs original C function: js_init_module_os() to load the os module....

https://github.com/ftk/quickjspp/blob/master/test/class.cpp

however, seems there is also a fair amount of rigaramole going on there to get that function to work with quickjspp context...

my question is there a simpler way to import the os module?

I want to use the context outside of main()

@ftk
Copy link
Owner

ftk commented Jul 9, 2022

What do you mean?
js_init_module_os(context.ctx, "os");
Also see https://github.com/ftk/quickjspp/blob/master/qjs.cpp

@drschwabe
Copy link
Author

hey thx a ton actually your one line example there was key for helping to solve this at least for my case.

I had previously gotten tripped up with scope and declaring new variables and pointers for the ctx object; supplying it directly as context.ctx skips the need to do the extra 'rigamarole'.

Here is my example, not sure if its 'simpler' but it does let one re-use the same context outside of main ie- in other functions across the program.

#include "quickjspp.hpp" 
#include "quickjs/quickjs-libc.h"

qjs::Runtime runtime;
qjs::Context js(runtime);

int main() {
  try {
    js_std_init_handlers(runtime.rt);
    js_init_module_os(js.ctx, "os");
    js_init_module_os(js.ctx, "std");  
    js.eval(R"xxx(     
      import * as os from 'os'
      globalThis.os = os
    )xxx", "<import>", JS_EVAL_TYPE_MODULE);
  } catch (qjs::exception) {
    handleJsErr(js.getException()); 
  } 
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants