quickjs icon indicating copy to clipboard operation
quickjs copied to clipboard

Return missing in module

Open nv08 opened this issue 1 year ago • 2 comments

function main() { return 10; }

main();

Where does the return exactly goes?

i was trying to get the result of return for this program

import add from "a.js";

function main() {
  const a = add(1, 2);
  console.log(a);
  return a;
}

main();

using jS_EVAL -> JS_EVAL_FUNCTION -> JS_STD_AWAIT after these i get "undefined" in value. Am i missing anything or its the default behaviour?

nv08 avatar Oct 01 '24 04:10 nv08

What's the contents of the "a.js" file?

johnjg75dev avatar Oct 05 '24 06:10 johnjg75dev

What's the contents of the "a.js" file?

Simple add function which add two numbers and return output

nv08 avatar Oct 05 '24 10:10 nv08

static int eval_buf(JSContext *ctx, const void *buf, int buf_len, const char *filename, int eval_flags) { JSValue val; int ret;

if ((eval_flags & JS_EVAL_TYPE_MASK) == JS_EVAL_TYPE_MODULE) {
    /* for the modules, we compile then run to be able to set
       import.meta */
    val = JS_Eval(ctx, buf, buf_len, filename,
                  eval_flags | JS_EVAL_FLAG_COMPILE_ONLY);
    if (!JS_IsException(val)) {
        js_module_set_import_meta(ctx, val, TRUE, TRUE);
        val = JS_EvalFunction(ctx, val);
    }
    val = js_std_await(ctx, val);
} else {
    val = JS_Eval(ctx, buf, buf_len, filename, eval_flags);
}
if (JS_IsException(val)) {
    js_std_dump_error(ctx);
    ret = -1;
} else {
    ret = 0;
}
JS_FreeValue(ctx, val);
return ret;

}

using this eval_buf, for non_module it works and returns as expected but for module (.js using import statements) it doesn't returns anything @johnjg75dev @ctn-malone

nv08 avatar Nov 04 '24 15:11 nv08

A module asynchronously returns either undefined or an exception.

bellard avatar Mar 13 '25 17:03 bellard