|
@@ -2417,16 +2417,13 @@ func (r *Runtime) Set(name string, value interface{}) error {
|
|
// Note, this is not the same as GlobalObject().Get(name),
|
|
// Note, this is not the same as GlobalObject().Get(name),
|
|
// because if a global lexical binding (let or const) exists, it is used instead.
|
|
// 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.
|
|
// 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.
|
|
// 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
|
|
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 {
|
|
func (r *Runtime) try(f func()) error {
|
|
if ex := r.vm.try(f); ex != nil {
|
|
if ex := r.vm.try(f); ex != nil {
|
|
return ex
|
|
return ex
|
|
@@ -2592,12 +2596,6 @@ func (r *Runtime) try(f func()) error {
|
|
return nil
|
|
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 {
|
|
func (r *Runtime) toObject(v Value, args ...interface{}) *Object {
|
|
if obj, ok := v.(*Object); ok {
|
|
if obj, ok := v.(*Object); ok {
|
|
return obj
|
|
return obj
|
|
@@ -3182,3 +3180,9 @@ func assertCallable(v Value) (func(FunctionCall) Value, bool) {
|
|
}
|
|
}
|
|
return nil, false
|
|
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)
|
|
|
|
+}
|