Comments (11)
This is further addressed in 83c56dd
I've added a .Call method to Otto, which allows you to call arbitrary JavaScript and return the result.
You can now do something like this in your Go code:
value, _ := call.Otto.Call("new Error", nil, "Something bad happened.")
panic(value)
If you want, a possible convenience function is:
func throw(value Value, _ error) Value {
panic(value)
return UndefinedValue()
}
With these two, you can do:
return throw(call.Otto.Call("new Error", nil, "Something bad happened.")
And it'll throw that exception in the JavaScript environment like you would expect
from otto.
Yes, this is something that needs work. Do you have any examples of what you're trying to achieve?
from otto.
FWIW, an example, schematically:
js helper
function mkfn(obj, fn) {
var oldfn = obj[fn];
obj[fn] = function() {
var v = oldfn.apply(this, arguments);
if (v.constructor == Error) {
throw v;
};
return v;
};
}
Go errf
func (vm *VM) errf(e error) (r js.Value) {
if e == nil {
return js.UndefinedValue()
}
r, err := vm.jsvm.Run(fmt.Sprintf("new Error('%s')", template.JSEscapeString(e.Error())))
if err != nil {
log.Fatal(err)
}
return
}
Example native binding
func (vm *VM) dbArrays(o *js.Object, db *dbm.DB) {
const (
obj = "db"
method = "arrays"
)
if err := o.Set(method, func(call js.FunctionCall) (r js.Value) {
defer func() {
if e := recover(); e != nil {
r = vm.errf(fmt.Errorf("%s.%s: %v", obj, method, e))
}
}()
a, err := db.Arrays()
if err != nil {
panic(err)
}
o, err := vm.jsvm.Object(`new Object();`)
if err != nil {
panic(err)
}
vm.array(o, &a)
return o.Value()
}); err != nil {
panic(err)
}
if _, err := vm.mkfn.Call(js.UndefinedValue(), o, method); err != nil {
panic(err)
}
}
Full code: https://github.com/cznic/tmp/blob/14d211b7818be2225f12f80fa299f0176ae771cb/g/glue.go
from otto.
So what you're trying to do is throw a panic from the inner Go code to the other Go code?
from otto.
No, the machinery allows the javascript clients to catch errors reported by
Go native functions.
On May 19, 2013 6:56 PM, "Robert Krimen" [email protected] wrote:
So what you're trying to do is initiate a panic from the inner Go code to
the other Go code?—
Reply to this email directly or view it on GitHubhttps://github.com//issues/17#issuecomment-18120767
.
from otto.
Yes, I think a Go panic should be visible within a try { ... } catch { ... }, probably as a PanicError, with no extra work necessary.
I also think a (special) piercing panic should be possible, something that can bypass JavaScript.
from otto.
My example is not about Go panicking. It's about Go errors reporting. The
particular implementation reads data from a disk based database. Read
errors or format corruption may occur. The native funtion needs to throw a
javascript exception in that case - instead of returning, say invalid data
via the javasript wrapped native function - as that cannot support multiple
return values, which Go can.
On May 19, 2013 8:06 PM, "Robert Krimen" [email protected] wrote:
Yes, I think a Go panic should be visible within a try { ... } catch { ...
}, probably as a PanicError, with no extra work necessary.I also think a (special) piercing panic should be possible, something that
can bypass JavaScript.—
Reply to this email directly or view it on GitHubhttps://github.com//issues/17#issuecomment-18121942
.
from otto.
This is partially addressed in e2e79bb
If you panic() something of type otto.Value, then it will behave as if you did a "throw" in JavaScript.
For example: panic(Otto.ToValue("Hello, World."))
is the same as: throw "Hello, World.";
from otto.
Looks quite good to me. Thanks!
from otto.
Be careful when doing a raw panic of a complex object:
panic(someObject)
panic(valueOfAnObject)
The above may cause memory issues with the Go runtime, see #59
from otto.
If panic(someObject)
uses memory even after recover()
is called, that's a Go bug.
from otto.
Related Issues (20)
- Compile throws error HOT 3
- JS VM crashing go HOT 2
- Errors caused by different go versions HOT 3
- TestBinaryShiftOperation test fails HOT 31
- Parser don't allow leading 0 HOT 8
- `panic: strconv.ParseInt: parsing "toJSON": invalid syntax` on `ToValue` a `map` HOT 3
- new Date can't not parse datetime string HOT 2
- Memory is slowly increasing until OOM HOT 4
- "use strict" is not in effect HOT 7
- Overview of extending thrid party packages
- Implementation of the export and import keywords to support, amongst others, unit tests. HOT 3
- dates and timezone HOT 1
- How do I call a function that I extract from other javascript code
- Doe this framework support wsam? HOT 1
- fatal error: concurrent map write HOT 5
- How to obtain the values of jump related variables such as window.location.href?
- 多线程的时候会报错 HOT 2
- Code generator: AST ⇒ javascript code HOT 3
- Value of json expression is `undefined` HOT 1
- `let x=1` and `const x=1` don't work. HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from otto.