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

set EvalFlag function receive boolean argument #309

Merged
merged 1 commit into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion README_zh-cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. 如果你想在函数中返回参数,请在函数中复制参数。

Expand Down
27 changes: 13 additions & 14 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand All @@ -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
}
}

Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion quickjs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

}
Expand Down
Loading