MCPcopy Create free account
hub / github.com/buke/quickjs-go / Compile

Method Compile

context.go:1113–1136  ·  view source on GitHub ↗

Compile returns a compiled bytecode with given code.

(code string, opts ...EvalOption)

Source from the content-addressed store, hash-verified

1111
1112// Compile returns a compiled bytecode with given code.
1113func (ctx *Context) Compile(code string, opts ...EvalOption) ([]byte, error) {
1114 if !ctx.hasValidRef() {
1115 return nil, errOwnerAccessDenied
1116 }
1117 opts = append(opts, EvalFlagCompileOnly(true))
1118 val := ctx.Eval(code, opts...)
1119 defer val.Free()
1120
1121 var kSize C.size_t = 0
1122 ptr := C.JS_WriteObject(ctx.ref, &kSize, val.ref, C.int(C.JS_WRITE_OBJ_BYTECODE))
1123
1124 if ptr == nil {
1125 return nil, ctx.Exception()
1126 }
1127
1128 defer C.js_free(ctx.ref, unsafe.Pointer(ptr))
1129
1130 ret := make([]byte, C.int(kSize))
1131 if kSize > 0 {
1132 copy(ret, C.GoBytes(unsafe.Pointer(ptr), C.int(kSize)))
1133 }
1134
1135 return ret, nil
1136}
1137
1138// Compile returns a compiled bytecode with given filename.
1139func (ctx *Context) CompileFile(filePath string, opts ...EvalOption) ([]byte, error) {

Callers 6

TestContextModulesFunction · 0.95
LoadModuleMethod · 0.95
CompileFileMethod · 0.95

Calls 5

hasValidRefMethod · 0.95
EvalMethod · 0.95
ExceptionMethod · 0.95
EvalFlagCompileOnlyFunction · 0.85
FreeMethod · 0.45