lithdew / quickjs Goto Github PK
View Code? Open in Web Editor NEWGo bindings to QuickJS: a fast, small, and embeddable ES2020 JavaScript interpreter.
License: MIT License
Go bindings to QuickJS: a fast, small, and embeddable ES2020 JavaScript interpreter.
License: MIT License
Hey, your project seems really cool! I saw your the last activity was about ~8 months ago, is it still in development?
func TestName(t *testing.T) {
const code = `
var name = "test_hello";
function main() {
}
main();
`
runtime := quickjs.NewRuntime()
context := runtime.NewContext()
defer context.Free()
for {
val, err := context.Eval(code)
if err != nil {
fmt.Printf("%#v\n", err)
time.Sleep(time.Second)
continue
}
g:=context.Globals()
fmt.Println(g.Get("name"),val)
time.Sleep(time.Millisecond*10)
val.Free()
}
}
test_hello undefined
test_hello undefined
test_hello undefined
test_hello undefined
test_hello undefined
test_hello undefined
test_hello undefined
test_hello undefined
test_hello undefined
test_hello undefined
test_hello undefined
&quickjs.Error{Cause:"", Stack:" at code:1\n"}
&quickjs.Error{Cause:"", Stack:" at code:1\n"}
&quickjs.Error{Cause:"", Stack:" at code:1\n"}
&quickjs.Error{Cause:"", Stack:" at code:1\n"}
&quickjs.Error{Cause:"", Stack:" at code:1\n"}
&quickjs.Error{Cause:"", Stack:" at code:1\n"}
&quickjs.Error{Cause:"", Stack:" at code:1\n"}
&quickjs.Error{Cause:"", Stack:" at code:1\n"}
&quickjs.Error{Cause:"", Stack:" at code:1\n"}
&quickjs.Error{Cause:"", Stack:" at code:1\n"}
&quickjs.Error{Cause:"", Stack:" at code:1\n"}
&quickjs.Error{Cause:"", Stack:" at code:1\n"}
&quickjs.Error{Cause:"", Stack:" at code:1\n"}
&quickjs.Error{Cause:"", Stack:" at code:1\n"}
&quickjs.Error{Cause:"", Stack:" at code:1\n"}
&quickjs.Error{Cause:"", Stack:" at code:1\n"}
&quickjs.Error{Cause:"", Stack:" at code:1\n"}
&quickjs.Error{Cause:"", Stack:" at code:1\n"}
&quickjs.Error{Cause:"", Stack:" at code:1\n"}
&quickjs.Error{Cause:"", Stack:" at code:1\n"}
&quickjs.Error{Cause:"", Stack:" at code:1\n"}
&quickjs.Error{Cause:"", Stack:" at code:1\n"}
test_hello undefined
The suspicion is of possible misuse of JS_NewCFunctionData
, which is used over JS_NewCFunction
to provide an opaque pointer to proxy()
in Go to dispatch Go functions in C.
It may be that this is not the intended use of JS_NewCFunctionData
, and that calling JS_FreeRuntime
frees unintended memory as a result.
func main() {
runtime := quickjs.NewRuntime()
defer runtime.Free()
ctx := runtime.NewContext()
defer ctx.Free()
globals := ctx.Globals()
defer globals.Free()
httpFunc := ctx.Function(func(ctx quickjs.Context, this quickjs.Value, args []quickjs.Value) quickjs.Value {
if len(args) != 1 || !args[0].IsString() {
return ctx.ThrowTypeError("http(request: string)")
}
request := args[0]
fmt.Println(request.String())
return request
})
defer httpFunc.Free()
globals.Set("http", httpFunc)
result, err := ctx.Eval(`http('test')`)
check(err)
defer result.Free()
fmt.Println(result)
}
$ CGO_CFLAGS="-DDUMP_LEAKS" go run main.go
# command-line-arguments
/tmp/go-link-072279044/000024.o: In function `_cgo_26061493d47f_C2func_getaddrinfo':
cgo_unix.cgo2.c:(.text+0x81): warning: Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
test
test
main: quickjs.c:5625: gc_decref_child: Assertion `p->ref_count > 0' failed.
SIGABRT: abort
PC=0x782978 m=0 sigcode=18446744073709551610
goroutine 0 [idle]:
runtime: unknown pc 0x782978
stack: frame={sp:0x7ffd922906b8, fp:0x0} stack=[0x7ffd91a91ae8,0x7ffd92290b10)
00007ffd922905b8: 000000000092db5c 0000000000000000
00007ffd922905c8: 0000000000798780 00000000fbad8000
00007ffd922905d8: 00000000022c1a00 00000000022c1a00
00007ffd922905e8: 00000000022c1a00 00000000022c1a00
00007ffd922905f8: 00000000022c1a4c 00000000022c1a64
00007ffd92290608: 00000000022c1a00 00000000022c1a64
00007ffd92290618: 0000000000000000 0000000000000000
00007ffd92290628: 0000000000000000 0000000000000000
00007ffd92290638: 0000000000000000 0000000092290660
00007ffd92290648: 0000000000688a26 00000000022c0000
00007ffd92290658: 0000000000000000 00007ffd92290690
00007ffd92290668: 0000000000688aae ffffffffffffffff
00007ffd92290678: 0000000000000000 00000000022b70c8
00007ffd92290688: 00007f356e578000 00000000009b2420
00007ffd92290698: 000000000092d5c4 00000000000015f9
00007ffd922906a8: 000000000092db5c 0000000000000000
00007ffd922906b8: <0000000000782d0a 0000000000000020
00007ffd922906c8: 0000000000000000 0000000000000000
00007ffd922906d8: 0000000000000000 0000000000000000
00007ffd922906e8: 0000000000000000 0000000000000000
00007ffd922906f8: 0000000000000000 0000000000000000
00007ffd92290708: 0000000000000000 0000000000000000
00007ffd92290718: 0000000000000000 0000000000000000
00007ffd92290728: 0000000000000000 0000000000000000
00007ffd92290738: 0000000000000000 0000000000000004
00007ffd92290748: 00000000022b7ab0 00007ffd92290720
00007ffd92290758: 00000016022c9230 0000000000000000
00007ffd92290768: 00007f356e578000 00000000009b2420
00007ffd92290778: 000000000092d5c4 00000000000015f9
00007ffd92290788: 000000000092db5c 0000000000000000
00007ffd92290798: 00000000007a5377 00000000022c3bf0
00007ffd922907a8: fffffffffffffff9 00000000009c9e9c
runtime: unknown pc 0x782978
stack: frame={sp:0x7ffd922906b8, fp:0x0} stack=[0x7ffd91a91ae8,0x7ffd92290b10)
00007ffd922905b8: 000000000092db5c 0000000000000000
00007ffd922905c8: 0000000000798780 00000000fbad8000
00007ffd922905d8: 00000000022c1a00 00000000022c1a00
00007ffd922905e8: 00000000022c1a00 00000000022c1a00
00007ffd922905f8: 00000000022c1a4c 00000000022c1a64
00007ffd92290608: 00000000022c1a00 00000000022c1a64
00007ffd92290618: 0000000000000000 0000000000000000
00007ffd92290628: 0000000000000000 0000000000000000
00007ffd92290638: 0000000000000000 0000000092290660
00007ffd92290648: 0000000000688a26 00000000022c0000
00007ffd92290658: 0000000000000000 00007ffd92290690
00007ffd92290668: 0000000000688aae ffffffffffffffff
00007ffd92290678: 0000000000000000 00000000022b70c8
00007ffd92290688: 00007f356e578000 00000000009b2420
00007ffd92290698: 000000000092d5c4 00000000000015f9
00007ffd922906a8: 000000000092db5c 0000000000000000
00007ffd922906b8: <0000000000782d0a 0000000000000020
00007ffd922906c8: 0000000000000000 0000000000000000
00007ffd922906d8: 0000000000000000 0000000000000000
00007ffd922906e8: 0000000000000000 0000000000000000
00007ffd922906f8: 0000000000000000 0000000000000000
00007ffd92290708: 0000000000000000 0000000000000000
00007ffd92290718: 0000000000000000 0000000000000000
00007ffd92290728: 0000000000000000 0000000000000000
00007ffd92290738: 0000000000000000 0000000000000004
00007ffd92290748: 00000000022b7ab0 00007ffd92290720
00007ffd92290758: 00000016022c9230 0000000000000000
00007ffd92290768: 00007f356e578000 00000000009b2420
00007ffd92290778: 000000000092d5c4 00000000000015f9
00007ffd92290788: 000000000092db5c 0000000000000000
00007ffd92290798: 00000000007a5377 00000000022c3bf0
00007ffd922907a8: fffffffffffffff9 00000000009c9e9c
goroutine 1 [syscall]:
runtime.cgocall(0x6578e2, 0xc000059db0, 0x1f9)
/snap/go/current/src/runtime/cgocall.go:133 +0x5b fp=0xc000059d80 sp=0xc000059d48 pc=0x403fab
github.com/lithdew/quickjs._Cfunc_JS_FreeRuntime(0x22b7030)
_cgo_gotypes.go:260 +0x41 fp=0xc000059db0 sp=0xc000059d80 pc=0x504551
github.com/lithdew/quickjs.Runtime.Free.func1(0x22b7030)
/home/kenta/lithdew/quickjs/quickjs.go:33 +0x4d fp=0xc000059de0 sp=0xc000059db0 pc=0x50691d
github.com/lithdew/quickjs.Runtime.Free(0x22b7030)
/home/kenta/lithdew/quickjs/quickjs.go:33 +0x2b fp=0xc000059df8 sp=0xc000059de0 pc=0x50556b
main.main()
/home/kenta/lithdew/ghost/main.go:109 +0x40e fp=0xc000059f88 sp=0xc000059df8 pc=0x656d2e
runtime.main()
/snap/go/current/src/runtime/proc.go:203 +0x1fa fp=0xc000059fe0 sp=0xc000059f88 pc=0x4359fa
runtime.goexit()
/snap/go/current/src/runtime/asm_amd64.s:1373 +0x1 fp=0xc000059fe8 sp=0xc000059fe0 pc=0x4620d1
rax 0x0
rbx 0x7f356e578000
rcx 0x782978
rdx 0x6
rdi 0x6960
rsi 0x6960
rbp 0x9b2420
rsp 0x7ffd922906b8
r8 0x22c1a00
r9 0x0
r10 0x8
r11 0x206
r12 0x92d5c4
r13 0x15f9
r14 0x92db5c
r15 0x0
rip 0x782978
rflags 0x206
cs 0x33
fs 0x0
gs 0x0
exit status 2
func main() {
runtime := quickjs.NewRuntime()
defer runtime.Free()
ctx := runtime.NewContext()
defer ctx.Free()
globals := ctx.Globals()
defer globals.Free()
httpFunc := ctx.Function(func(ctx quickjs.Context, this quickjs.Value, args []quickjs.Value) quickjs.Value {
if len(args) != 1 || !args[0].IsString() {
return ctx.ThrowTypeError("http(request: string)")
}
request := args[0]
fmt.Println(request.String())
return request
})
//defer httpFunc.Free()
globals.Set("http", httpFunc)
result, err := ctx.Eval(`http('test')`)
check(err)
//defer result.Free()
fmt.Println(result)
}
$ CGO_CFLAGS="-DDUMP_LEAKS" go run main.go
# command-line-arguments
/tmp/go-link-896294616/000024.o: In function `_cgo_26061493d47f_C2func_getaddrinfo':
cgo_unix.cgo2.c:(.text+0x81): warning: Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
test
test
fatal error: unexpected signal during runtime execution
[signal SIGSEGV: segmentation violation code=0x2 addr=0x1e4c030 pc=0x1e4c030]
runtime stack:
runtime.throw(0x8bd124, 0x2a)
/snap/go/current/src/runtime/panic.go:1116 +0x72
runtime.sigpanic()
/snap/go/current/src/runtime/signal_unix.go:679 +0x46a
goroutine 1 [syscall]:
runtime.cgocall(0x6577e2, 0xc000059e08, 0x1f9)
/snap/go/current/src/runtime/cgocall.go:133 +0x5b fp=0xc000059dd8 sp=0xc000059da0 pc=0x403fab
github.com/lithdew/quickjs._Cfunc_JS_FreeRuntime(0x1e38010)
_cgo_gotypes.go:260 +0x41 fp=0xc000059e08 sp=0xc000059dd8 pc=0x504551
github.com/lithdew/quickjs.Runtime.Free.func1(0x1e38010)
/home/kenta/lithdew/quickjs/quickjs.go:33 +0x4d fp=0xc000059e38 sp=0xc000059e08 pc=0x50691d
github.com/lithdew/quickjs.Runtime.Free(0x1e38010)
/home/kenta/lithdew/quickjs/quickjs.go:33 +0x2b fp=0xc000059e50 sp=0xc000059e38 pc=0x50556b
main.main()
/home/kenta/lithdew/ghost/main.go:109 +0x329 fp=0xc000059f88 sp=0xc000059e50 pc=0x656c49
runtime.main()
/snap/go/current/src/runtime/proc.go:203 +0x1fa fp=0xc000059fe0 sp=0xc000059f88 pc=0x4359fa
runtime.goexit()
/snap/go/current/src/runtime/asm_amd64.s:1373 +0x1 fp=0xc000059fe8 sp=0xc000059fe0 pc=0x4620d1
exit status 2
Thanks for putting this package together! It works great out of the box without any additional installation scripts.
I was wondering about Guideline 5.
Make sure to call runtime.LockOSThread() to ensure that QuickJS always operates in the exact same thread.
Does this make QuickJS unsuitable to run inside http.Handler
s where you can have many goroutines servicing requests at the same time? The use-case in mind is server-side rendering.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.