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

不能进行json反序列化 #51

Closed
honey-yogurt opened this issue Apr 23, 2022 · 2 comments
Closed

不能进行json反序列化 #51

honey-yogurt opened this issue Apr 23, 2022 · 2 comments
Labels
question Further information is requested

Comments

@honey-yogurt
Copy link

image

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

Load error: unresolve external:encoding/json.Unmarshal

但是可以进行序列化

@pkujhd
Copy link
Owner

pkujhd commented Apr 24, 2022

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

@pkujhd pkujhd closed this as completed Apr 24, 2022
@pkujhd pkujhd added the question Further information is requested label Apr 24, 2022
@eh-steve
Copy link

eh-steve commented Nov 5, 2022

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))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants