Browse Source

Introduced Runtime.Try, and Runtime.InstanceOf. Closes #268.

Dmitry Panov 2 years ago
parent
commit
b68b7457e4
2 changed files with 22 additions and 22 deletions
  1. 20 16
      runtime.go
  2. 2 6
      value.go

+ 20 - 16
runtime.go

@@ -2417,16 +2417,13 @@ func (r *Runtime) Set(name string, value interface{}) error {
 // Note, this is not the same as GlobalObject().Get(name),
 // because if a global lexical binding (let or const) exists, it is used instead.
 // This method will panic with an *Exception if a JavaScript exception is thrown in the process.
-func (r *Runtime) Get(name string) (ret Value) {
-	r.tryPanic(func() {
-		n := unistring.NewFromString(name)
-		if v, exists := r.global.stash.getByName(n); exists {
-			ret = v
-		} else {
-			ret = r.globalObject.self.getStr(n, nil)
-		}
-	})
-	return
+func (r *Runtime) Get(name string) Value {
+	n := unistring.NewFromString(name)
+	if v, exists := r.global.stash.getByName(n); exists {
+		return v
+	} else {
+		return r.globalObject.self.getStr(n, nil)
+	}
 }
 
 // SetRandSource sets random source for this Runtime. If not called, the default math/rand is used.
@@ -2585,6 +2582,13 @@ func tryFunc(f func()) (ret interface{}) {
 	return
 }
 
+// Try runs a given function catching and returning any JS exception. Use this method to run any code
+// that may throw exceptions (such as Object.Get, Object.String, Object.ToInteger, Object.Export, Runtime.Get, Runtime.InstanceOf, etc.)
+// outside the Runtime execution context (i.e. when calling directly from Go, not from a JS function implemented in Go).
+func (r *Runtime) Try(f func()) *Exception {
+	return r.vm.try(f)
+}
+
 func (r *Runtime) try(f func()) error {
 	if ex := r.vm.try(f); ex != nil {
 		return ex
@@ -2592,12 +2596,6 @@ func (r *Runtime) try(f func()) error {
 	return nil
 }
 
-func (r *Runtime) tryPanic(f func()) {
-	if ex := r.vm.try(f); ex != nil {
-		panic(ex)
-	}
-}
-
 func (r *Runtime) toObject(v Value, args ...interface{}) *Object {
 	if obj, ok := v.(*Object); ok {
 		return obj
@@ -3182,3 +3180,9 @@ func assertCallable(v Value) (func(FunctionCall) Value, bool) {
 	}
 	return nil, false
 }
+
+// InstanceOf is an equivalent of "left instanceof right".
+// This method will panic with an *Exception if a JavaScript exception is thrown in the process.
+func (r *Runtime) InstanceOf(left Value, right *Object) (res bool) {
+	return instanceOfOperator(left, right)
+}

+ 2 - 6
value.go

@@ -770,12 +770,8 @@ func (o *Object) baseObject(*Runtime) *Object {
 // In all other cases returns own enumerable non-symbol properties as map[string]interface{}.
 //
 // This method will panic with an *Exception if a JavaScript exception is thrown in the process.
-func (o *Object) Export() (ret interface{}) {
-	o.runtime.tryPanic(func() {
-		ret = o.self.export(&objectExportCtx{})
-	})
-
-	return
+func (o *Object) Export() interface{} {
+	return o.self.export(&objectExportCtx{})
 }
 
 // ExportType returns the type of the value that is returned by Export().