Skip to content

Commit

Permalink
add resource free
Browse files Browse the repository at this point in the history
  • Loading branch information
wspl committed Jul 15, 2019
1 parent 1d4d95b commit 68c87ca
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 73 deletions.
10 changes: 6 additions & 4 deletions context-raw.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package quickjs

// #cgo CFLAGS: -I.
// #cgo LDFLAGS: -L. -lquickjs
// #include <quickjs-libc.h>
/*
#cgo CFLAGS: -I.
#cgo LDFLAGS: -L. -lquickjs
#include "quickjs-bridge.h"
*/
import "C"

type JSContextRaw struct {
Expand Down Expand Up @@ -63,4 +65,4 @@ func (ctx *JSContextRaw) AddIntrinsicTypedArrays() {

func (ctx *JSContextRaw) AddIntrinsicPromise() {
C.JS_AddIntrinsicPromise(ctx.ref)
}
}
71 changes: 57 additions & 14 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ package quickjs
#include "quickjs-bridge.h"
*/
import "C"
import "unsafe"
import (
"unsafe"
)

var ctxMap = make(map[*C.JSContext]*JSContext)

Expand All @@ -29,6 +31,20 @@ func NewJSContext(runtime *JSRuntime) *JSContext {
return ctx
}

func (ctx *JSContext) FreeCValue(value C.JSValue) {
C.JS_FreeValue(ctx.ref, value)
}

func (ctx *JSContext) FreeValue(value *JSValue) {
C.JS_FreeValue(ctx.ref, value.ref)
}

func (ctx *JSContext) Free() {
ctx.FreeCValue(ctx.cFunction)
ctx.global.Free()
C.JS_FreeContext(ctx.ref)
}

func (ctx *JSContext) Eval(script string, filename string) (*JSValue, *JSError) {
scriptCstr := C.CString(script)
defer C.free(unsafe.Pointer(scriptCstr))
Expand All @@ -45,6 +61,35 @@ func (ctx *JSContext) Eval(script string, filename string) (*JSValue, *JSError)
return ret, nil
}

func (ctx *JSContext) EvalBinary(buf []byte) (*JSValue, *JSError) {
ret := ctx.WrapValue(C.JS_EvalBinary(ctx.ref, (*C.uchar)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)), C.int(0)))
e := ctx.Exception()
if e != nil {
return ret, e
}
return ret, nil
}

func (ctx *JSContext) Binary(script string, filename string) []byte {
scriptCstr := C.CString(script)
defer C.free(unsafe.Pointer(scriptCstr))
scriptClen := C.ulong(len(script))

filenameCstr := C.CString(filename)
defer C.free(unsafe.Pointer(filenameCstr))

obj := C.JS_Eval(
ctx.ref,
scriptCstr,
scriptClen,
filenameCstr,
C.JS_EVAL_FLAG_SHEBANG|C.JS_EVAL_FLAG_COMPILE_ONLY|C.JS_EVAL_TYPE_GLOBAL,
)
outBufLen := C.size_t(0)
outBuf := C.JS_WriteObject(ctx.ref, &outBufLen, obj, C.JS_WRITE_OBJ_BYTECODE)
return C.GoBytes(unsafe.Pointer(outBuf), C.int(outBufLen))
}

func (ctx *JSContext) Global() *JSValue {
return ctx.global
}
Expand Down Expand Up @@ -73,6 +118,10 @@ func (ctx *JSContext) NewFloat64(double float64) *JSValue {
return ctx.WrapValue(C.JS_NewFloat64(ctx.ref, C.double(double)))
}

func (ctx *JSContext) NewObject() *JSValue {
return ctx.WrapValue(C.JS_NewObject(ctx.ref))
}

func (ctx *JSContext) NewString(string string) *JSValue {
cstr := C.CString(string)
defer C.free(unsafe.Pointer(cstr))
Expand Down Expand Up @@ -112,18 +161,12 @@ func (ctx *JSContext) Exception() *JSError {
}

func (ctx *JSContext) Try() {
fn := ctx.NewGoFunction(func(args []*JSValue, this *JSValue) (*JSValue, *JSError) {
return ctx.NewString("Hello"), nil
})
ret := fn.Call([]*JSValue{ctx.NewString("Arg1"), ctx.NewBool(true)}, nil)
obj := ctx.NewObject()
obj.SetProperty("a", ctx.NewInt64(22444))
obj.SetProperty("b", ctx.NewString("Hello"))
obj.Expose("obj")
buf := ctx.Binary("JSON.stringify(obj)", "")
println(string(buf))
ret, _ := ctx.EvalBinary(buf)
println(ret.String())
//ret, err := ctx.Eval("as", "")
//if err != nil {
// println("1", err.Message())
//}
//ret, err = ctx.Eval("const a = 1", "")
//if err != nil {
// println("2", err.Message())
//}
//println(ret.String())
}
3 changes: 2 additions & 1 deletion error.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package quickjs

/*
#cgo CFLAGS: -I.
#cgo LDFLAGS: -L. -lquickjs
Expand Down Expand Up @@ -32,4 +33,4 @@ func (jse *JSError) Message() string {

func (jse *JSError) Errno() string {
return jse.Value().Property("errno").String()
}
}
45 changes: 5 additions & 40 deletions main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,12 @@ import (
. "go-quickjs"
)

func main () {
//a := []uint8 {
// 0x01, 0x04, 0x0e, 0x63, 0x6f, 0x6e, 0x73, 0x6f,
// 0x6c, 0x65, 0x06, 0x6c, 0x6f, 0x67, 0x16, 0x48,
// 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72,
// 0x6c, 0x64, 0x08, 0x61, 0x2e, 0x6a, 0x73, 0x0d,
// 0x00, 0x02, 0x00, 0x9e, 0x01, 0x00, 0x01, 0x00,
// 0x03, 0x00, 0x00, 0x14, 0x01, 0xa0, 0x01, 0x00,
// 0x00, 0x00, 0x38, 0xc4, 0x00, 0x00, 0x00, 0x42,
// 0xc5, 0x00, 0x00, 0x00, 0x04, 0xc6, 0x00, 0x00,
// 0x00, 0x27, 0x01, 0x00, 0xd2, 0x2b, 0x8e, 0x03,
// 0x01, 0x00,
//}
//
//rt := C.JS_NewRuntime()
//ctx := C.JS_NewContextRaw(rt)
//
//C.JS_AddIntrinsicBaseObjects(ctx)
//C.JS_AddIntrinsicEval(ctx)
//C.JS_AddIntrinsicStringNormalize(ctx)
//C.JS_AddIntrinsicRegExp(ctx)
//C.JS_AddIntrinsicJSON(ctx)
//C.JS_AddIntrinsicProxy(ctx)
//C.JS_AddIntrinsicMapSet(ctx)
//C.JS_AddIntrinsicTypedArrays(ctx)
//C.JS_AddIntrinsicPromise(ctx)
//
//C.js_std_add_helpers(ctx, 0, nil)
//C.js_std_eval_binary(ctx, (*C.uchar)(unsafe.Pointer(&a[0])), (C.ulong)(len(a)), 0)
//C.js_std_loop(ctx)
//
//C.JS_FreeContext(ctx)
//C.JS_FreeRuntime(rt)

func main() {
runtime := NewJSRuntime()
context := NewJSContext(runtime)
//
//b := context.Eval("22.4123432", "1.js")
//println(b.Int32())
//
//println(context.NewNull().Bool())
context := runtime.NewContext()

context.Try()

context.Free()
runtime.Free()
}
10 changes: 6 additions & 4 deletions quickjs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package quickjs

// #cgo CFLAGS: -I.
// #cgo LDFLAGS: -L. -lquickjs
// #include <quickjs-libc.h>
import "C"
/*
#cgo CFLAGS: -I.
#cgo LDFLAGS: -L. -lquickjs
#include "quickjs-bridge.h"
*/
import "C"
16 changes: 12 additions & 4 deletions runtime.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package quickjs

// #cgo CFLAGS: -I.
// #cgo LDFLAGS: -L. -lquickjs
// #include <quickjs-libc.h>
/*
#include "quickjs-bridge.h"
*/
import "C"

type JSRuntime struct {
Expand All @@ -13,4 +13,12 @@ func NewJSRuntime() *JSRuntime {
rt := new(JSRuntime)
rt.ref = C.JS_NewRuntime()
return rt
}
}

func (rt *JSRuntime) NewContext() *JSContext {
return NewJSContext(rt)
}

func (rt *JSRuntime) Free() {
C.JS_FreeRuntime(rt.ref)
}
21 changes: 15 additions & 6 deletions value.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import "unsafe"
/*
#cgo CFLAGS: -I.
#cgo LDFLAGS: -L. -lquickjs
#include <quickjs-libc.h>
#include "quickjs-bridge.h"
*/
import "C"

var valueMap = make(map[*C.JSValue]JSValue)

type JSValue struct {
ref C.JSValue
ctx *JSContext
Expand All @@ -22,6 +23,10 @@ func NewJSValue(ctx *JSContext, cval C.JSValue) *JSValue {
return val
}

func (val *JSValue) Free() {
C.JS_FreeValue(val.ctx.ref, val.ref)
}

func (val *JSValue) AttachContext(ctx *JSContext) {
val.ctx = ctx
}
Expand Down Expand Up @@ -142,22 +147,26 @@ func (val *JSValue) Call(args []*JSValue, this *JSValue) *JSValue {
return val.ctx.WrapValue(C.JS_Call(val.ctx.ref, val.ref, this.ref, C.int(len(args)), &cargs[0]))
}

func (val *JSValue) PropertyByInt (key int) *JSValue {
func (val *JSValue) PropertyByInt(key int) *JSValue {
return val.ctx.WrapValue(C.JS_GetPropertyUint32(val.ctx.ref, val.ref, C.uint32_t(key)))
}

func (val *JSValue) Property (key string) *JSValue {
func (val *JSValue) Property(key string) *JSValue {
cstr := C.CString(key)
defer C.free(unsafe.Pointer(cstr))
return val.ctx.WrapValue(C.JS_GetPropertyStr(val.ctx.ref, val.ref, cstr))
}

func (val *JSValue) SetPropertyByInt (key int, value *JSValue) {
func (val *JSValue) SetPropertyByInt(key int, value *JSValue) {
C.JS_SetPropertyInt64(val.ctx.ref, val.ref, C.int64_t(key), value.ref)
}

func (val *JSValue) SetProperty (key string, value *JSValue) {
func (val *JSValue) SetProperty(key string, value *JSValue) {
cstr := C.CString(key)
defer C.free(unsafe.Pointer(cstr))
C.JS_SetPropertyStr(val.ctx.ref, val.ref, cstr, value.ref)
}
}

func (val *JSValue) Expose(name string) {
val.ctx.global.SetProperty(name, val)
}

0 comments on commit 68c87ca

Please sign in to comment.