From 7aeaf260f45fd2ce37b4a76e085dc99b9ce3a1a7 Mon Sep 17 00:00:00 2001 From: wangbuke Date: Tue, 18 Jun 2024 11:18:11 +0800 Subject: [PATCH] set EvalFlag function receive boolean argument --- README_zh-cn.md | 1 - context.go | 27 +++++++++++++-------------- quickjs_test.go | 2 +- 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/README_zh-cn.md b/README_zh-cn.md index 9cc0c8c..141e167 100644 --- a/README_zh-cn.md +++ b/README_zh-cn.md @@ -45,7 +45,6 @@ Go 语言的QuickJS绑定库:快速、小型、可嵌入的ES2020 JavaScript 1. 在使用完毕后,请记得关闭 `quickjs.Runtime` 和 `quickjs.Context`。 2. 请记得关闭由 `Eval()` 和 `EvalFile()` 返回的 `quickjs.Value`。其他值不需要关闭,因为它们会被垃圾回收。 3. 如果你使用了promise 或 async function,请使用 `ctx.Loop()` 等待所有的promise/job结果。 -4. You may access the stacktrace of an error returned by `Eval()` or `EvalFile()` by casting it to a `*quickjs.Error`. 4. 如果`Eval()` 或 `EvalFile()`返回了错误,可强制转换为`*quickjs.Error`以读取错误的堆栈信息。 5. 如果你想在函数中返回参数,请在函数中复制参数。 diff --git a/context.go b/context.go index 6c250b9..d0bd3f2 100644 --- a/context.go +++ b/context.go @@ -256,33 +256,33 @@ type EvalOptions struct { type EvalOption func(*EvalOptions) -func EvalFlagGlobal() EvalOption { +func EvalFlagGlobal(global bool) EvalOption { return func(flags *EvalOptions) { - flags.js_eval_type_global = true + flags.js_eval_type_global = global } } -func EvalFlagModule() EvalOption { +func EvalFlagModule(module bool) EvalOption { return func(flags *EvalOptions) { - flags.js_eval_type_module = true + flags.js_eval_type_module = module } } -func EvalFlagStrict() EvalOption { +func EvalFlagStrict(strict bool) EvalOption { return func(flags *EvalOptions) { - flags.js_eval_flag_strict = true + flags.js_eval_flag_strict = strict } } -func EvalFlagStrip() EvalOption { +func EvalFlagStrip(strip bool) EvalOption { return func(flags *EvalOptions) { - flags.js_eval_flag_strip = true + flags.js_eval_flag_strip = strip } } -func EvalFlagCompileOnly() EvalOption { +func EvalFlagCompileOnly(compileOnly bool) EvalOption { return func(flags *EvalOptions) { - flags.js_eval_flag_compile_only = true + flags.js_eval_flag_compile_only = compileOnly } } @@ -292,9 +292,9 @@ func EvalFileName(filename string) EvalOption { } } -func EvalAwait() EvalOption { +func EvalAwait(await bool) EvalOption { return func(flags *EvalOptions) { - flags.await = true + flags.await = await } } @@ -318,7 +318,6 @@ func (ctx *Context) Eval(code string, opts ...EvalOption) (Value, error) { if options.js_eval_type_module { cFlag |= C.JS_EVAL_TYPE_MODULE } - if options.js_eval_flag_strict { cFlag |= C.JS_EVAL_FLAG_STRICT } @@ -436,7 +435,7 @@ func (ctx *Context) EvalBytecode(buf []byte) (Value, error) { // Compile returns a compiled bytecode with given code. func (ctx *Context) Compile(code string, opts ...EvalOption) ([]byte, error) { - opts = append(opts, EvalFlagCompileOnly()) + opts = append(opts, EvalFlagCompileOnly(true)) val, err := ctx.Eval(code, opts...) if err != nil { return nil, err diff --git a/quickjs_test.go b/quickjs_test.go index 6d1028f..bb0edc0 100644 --- a/quickjs_test.go +++ b/quickjs_test.go @@ -777,7 +777,7 @@ func TestAwait(t *testing.T) { promiseAwait, _ := ctx.Await(promise) require.EqualValues(t, "Hello Await", promiseAwait.String()) - promiseAwaitEval, _ := ctx.Eval("testAsync('Hello ', 'AwaitEval')", quickjs.EvalAwait()) + promiseAwaitEval, _ := ctx.Eval("testAsync('Hello ', 'AwaitEval')", quickjs.EvalAwait(true)) require.EqualValues(t, "Hello AwaitEval", promiseAwaitEval.String()) }