LoadModule returns a js value with given code and module name.
(code string, moduleName string, opts ...EvalOption)
| 993 | |
| 994 | // LoadModule returns a js value with given code and module name. |
| 995 | func (ctx *Context) LoadModule(code string, moduleName string, opts ...EvalOption) *Value { |
| 996 | options := EvalOptions{ |
| 997 | load_only: false, |
| 998 | } |
| 999 | for _, fn := range opts { |
| 1000 | fn(&options) |
| 1001 | } |
| 1002 | |
| 1003 | codeBuf := zeroTerminatedBytes(code) |
| 1004 | codePtr := (*C.char)(unsafe.Pointer(&codeBuf[0])) |
| 1005 | |
| 1006 | var pinner goruntime.Pinner |
| 1007 | pinner.Pin(&codeBuf[0]) |
| 1008 | defer pinner.Unpin() |
| 1009 | |
| 1010 | isModule := ctx.detectModuleSource(code, codePtr) |
| 1011 | goruntime.KeepAlive(codeBuf) |
| 1012 | if !isModule { |
| 1013 | return ctx.ThrowSyntaxError("not a module: %s", moduleName) |
| 1014 | } |
| 1015 | |
| 1016 | var ( |
| 1017 | codeByte []byte |
| 1018 | err error |
| 1019 | ) |
| 1020 | if loadModuleCompileHook != nil { |
| 1021 | codeByte, err = loadModuleCompileHook(ctx, code, moduleName) |
| 1022 | } else { |
| 1023 | codeByte, err = ctx.Compile(code, EvalFlagModule(true), EvalFlagCompileOnly(true), EvalFileName(moduleName)) |
| 1024 | } |
| 1025 | if err != nil { |
| 1026 | return ctx.ThrowError(err) |
| 1027 | } |
| 1028 | |
| 1029 | return ctx.LoadModuleBytecode(codeByte, EvalLoadOnly(options.load_only)) |
| 1030 | |
| 1031 | } |
| 1032 | |
| 1033 | // LoadModuleFile returns a js value with given file path and module name. |
| 1034 | func (ctx *Context) LoadModuleFile(filePath string, moduleName string) *Value { |