Compile returns a compiled bytecode with given code.
(code string, opts ...EvalOption)
| 1111 | |
| 1112 | // Compile returns a compiled bytecode with given code. |
| 1113 | func (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. |
| 1139 | func (ctx *Context) CompileFile(filePath string, opts ...EvalOption) ([]byte, error) { |