Exemple #1
0
// EvaluateScript evaluates the JavaScript code in script.
func (ctx *Context) EvaluateScript(script string, thisObject *Object, sourceURL string, startingLineNumber int) (*Value, error) {
	scriptRef := NewString(script)
	defer scriptRef.Release()

	var sourceRef *String
	if sourceURL != "" {
		sourceRef = NewString(sourceURL)
		defer sourceRef.Release()
	}

	if thisObject == nil {
		thisObject = ctx.NewEmptyObject()
	}

	log.Println("About to evaluate script:", script, thisObject, sourceURL, startingLineNumber)

	errVal := ctx.newErrorValue()
	ret := C.JSEvaluateScript(ctx.ref,
		C.JSStringRef(unsafe.Pointer(scriptRef)), thisObject.ref,
		C.JSStringRef(unsafe.Pointer(sourceRef)), C.int(startingLineNumber), &errVal.ref)
	if ret == nil {
		// An error occurred
		// Error information should be stored in exception
		return nil, errVal
	}

	// Successful evaluation
	return ctx.newValue(ret), nil
}
Exemple #2
0
func (obj *Object) HasProperty(name string) bool {
	jsstr := NewString(name)
	defer jsstr.Release()

	ret := C.JSObjectHasProperty(obj.ctx.ref, obj.ref, C.JSStringRef(unsafe.Pointer(jsstr)))
	return bool(ret)
}
Exemple #3
0
func (obj *Object) DeleteProperty(name string) (bool, error) {
	jsstr := NewString(name)
	defer jsstr.Release()

	errVal := obj.ctx.newErrorValue()

	ret := C.JSObjectDeleteProperty(obj.ctx.ref, obj.ref, C.JSStringRef(unsafe.Pointer(jsstr)), &errVal.ref)
	if errVal.ref != nil {
		return false, errVal
	}

	return bool(ret), nil
}
Exemple #4
0
// CheckScriptSyntax checks the JavaScript syntax of script.
func (ctx *Context) CheckScriptSyntax(script string, sourceURL string, startingLineNumber int) error {
	scriptRef := NewString(script)
	defer scriptRef.Release()

	var sourceRef *String
	if sourceURL != "" {
		sourceRef = NewString(sourceURL)
		defer sourceRef.Release()
	}

	errVal := ctx.newErrorValue()
	ret := C.JSCheckScriptSyntax(ctx.ref,
		C.JSStringRef(unsafe.Pointer(scriptRef)), C.JSStringRef(unsafe.Pointer(sourceRef)),
		C.int(startingLineNumber), &errVal.ref)
	if !ret {
		// A syntax error was found
		// exception should be non-nil
		return errVal
	}

	// exception should be nil
	return nil
}
Exemple #5
0
func (obj *Object) SetProperty(name string, rhs *Value, attributes uint8) error {
	jsstr := NewString(name)
	defer jsstr.Release()

	errVal := obj.ctx.newErrorValue()

	C.JSObjectSetProperty(obj.ctx.ref, obj.ref, C.JSStringRef(unsafe.Pointer(jsstr)), rhs.ref,
		(C.JSPropertyAttributes)(attributes), &errVal.ref)
	if errVal.ref != nil {
		return errVal
	}

	return nil
}