pkujhd / goloader

load and run golang code at runtime.
Apache License 2.0
506 stars 58 forks source link

不能进行json反序列化 #51

Closed honey-yogurt closed 2 years ago

honey-yogurt commented 2 years ago

image

执行 ./loader -o hello.o -run main.Hello会报错:

Load error: unresolve external:encoding/json.Unmarshal

但是可以进行序列化

pkujhd commented 2 years ago

@zhiguogg 说明encoding/json.Unmarshal在你的loader里不存在这个符号,如果你的loader里没有调用过这个函数,那么需要调用goloader.RegTypes来完成注册,具体可以看examples

eh-steve commented 2 years ago

The JIT compiler in #66 will be able to import and build external packages for you automatically, so you don't have to register types on the loader side anymore.

See the tests in jit/jit_test.go for examples

You can simply do:


loadable, err = jit.BuildGoFiles(jit.BuildConfig{}, "./path/to/file1.go", "/path/to/file2.go")
// or
loadable, err = jit.BuildGoPackage(jit.BuildConfig{}, "./path/to/package")
// or
loadable, err = jit.BuildGoText(jit.BuildConfig{}, `
package mypackage

import "encoding/json"

func MyFunc(input []byte) (interface{}, error) {
    var output interface{}
    err := json.Unmarshal(input, &output)
    return output, err
}

`)

if err != nil {
    panic(err)
}

module, symbols, err = loadable.Load()

if err != nil {
    panic(err)
}

MyFunc := symbols["MyFunc"].(func([]byte) (interface{}, error))