Skip to content

Commit

Permalink
quickjs: add ThrowReferenceError, ThrowRangeError, ThrowInternalError
Browse files Browse the repository at this point in the history
  • Loading branch information
lithdew committed Jul 9, 2020
1 parent be5f53f commit 9e24121
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions quickjs.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,39 @@ func (c Context) ThrowTypeError(format string, args ...interface{}) Value {
return val
}

func (c Context) ThrowReferenceError(format string, args ...interface{}) Value {
cause := fmt.Sprintf(format, args...)

causePtr := C.CString(cause)
defer C.free(unsafe.Pointer(causePtr))

val := Value{ctx: c.ref, ref: C.ThrowReferenceError(c.ref, causePtr)}
runtime.SetFinalizer(&val, func(val *Value) { C.JS_FreeValue(val.ctx, val.ref) })
return val
}

func (c Context) ThrowRangeError(format string, args ...interface{}) Value {
cause := fmt.Sprintf(format, args...)

causePtr := C.CString(cause)
defer C.free(unsafe.Pointer(causePtr))

val := Value{ctx: c.ref, ref: C.ThrowRangeError(c.ref, causePtr)}
runtime.SetFinalizer(&val, func(val *Value) { C.JS_FreeValue(val.ctx, val.ref) })
return val
}

func (c Context) ThrowInternalError(format string, args ...interface{}) Value {
cause := fmt.Sprintf(format, args...)

causePtr := C.CString(cause)
defer C.free(unsafe.Pointer(causePtr))

val := Value{ctx: c.ref, ref: C.ThrowInternalError(c.ref, causePtr)}
runtime.SetFinalizer(&val, func(val *Value) { C.JS_FreeValue(val.ctx, val.ref) })
return val
}

func (c Context) Exception() Value {
val := Value{ctx: c.ref, ref: C.JS_GetException(c.ref)}
runtime.SetFinalizer(&val, func(val *Value) { C.JS_FreeValue(val.ctx, val.ref) })
Expand Down

0 comments on commit 9e24121

Please sign in to comment.