[quickjs-devel] Re: Confusion with JS_Call()

  • From: "Sam Chang" <dmarc-noreply@xxxxxxxxxxxxx> (Redacted sender "sam.chang" for DMARC)
  • To: quickjs-devel@xxxxxxxxxxxxx
  • Date: Thu, 29 Aug 2019 17:56:59 +0800

I think you just need to return the foo function on the first eval:

"function foo() { return 0; } foo;”

Regards,
Sam

On Aug 29, 2019, at 9:39 AM, Jerry Sievert <jerry@xxxxxxxxxxxxxxxxxxxxxx> 
wrote:

Hi,

I’ve read the list archives, but am still a little confused on how to set up 
a function that will work from C via JS_Call().

This is what I have:

#include <stdio.h>
#include <string.h>

#include "quickjs-libc.h"


int main() {
 JSRuntime *rt;
 JSContext *ctx;

 rt = JS_NewRuntime();
 if (rt == NULL) {
   fprintf(stderr, "Unable to allocate runtime\n");
   exit(2);
 }

 ctx = JS_NewContext(rt);
 if (ctx == NULL) {
   fprintf(stderr, "Unable to allocate context\n");
   exit(2);
 }

 const char *js = "function foo() { return 0; }";

 JSValue val = JS_Eval(ctx, js, strlen(js), "<function foo>", 0);
 if (JS_IsException(val)) {
   js_std_dump_error(ctx);
 }

 JSValue val2 = JS_Call(ctx, val, JS_UNDEFINED, 0, NULL);
 if (JS_IsException(val2)) {
     js_std_dump_error(ctx);
 }
 JS_FreeValue(ctx, val);
 JS_FreeValue(ctx, val2);
 js_std_loop(ctx);

 return 0;
}



And I’ve tried multiple values for the last argument in JS_Eval(), and 
obviously JS_EVAL_FLAG_COMPILE_ONLY isn’t what I want, but I seem to be 
unable to find a flag that returns a function, and thus end up with 
"TypeError: not a function” as the result of JS_Call().

Any help would be appreciated, thanks!


Other related posts: