Comments (6)
This is because currently for Go values that implement fmt.Stringer
(and some others), toString
and valueOf
methods are added directly on the returned instance because otherwise it would use Object.prototype.toString
which would return [object Object]
.
The easiest workaround is to delete the toString
method from the instance before returning (by calling res.Delete("toString")
). Not ideal, but I can't think of a better solution right now.
An alternative would be to create a special class GoObject
which extends Object
and use it as a prototype for wrapped values... Maybe not add it to the global object, similarly to the TypedArray
constructor. It would give a benefit of being able to detect these values in JS code... I'll give it a thought.
from goja.
Hmm deleting would be a nice little work around for now. Unfortunately it doesn't seem to do anything. I've done the following:
res := r.ToValue(u).(*goja.Object)
err := res.Delete("toString")
if err != nil {
panic("Failed to delete toString()")
}
res.SetPrototype(call.This.Prototype())
The code above returns a nil value to err
yet if I call res.Get("toString")
I still see the value set, and inspecting the value after the delete call shows that it's still set.
![image](https://private-user-images.githubusercontent.com/647560/242756512-b7b6e46e-d4b6-4b5f-90af-a8940440157d.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTk0MTMyMjYsIm5iZiI6MTcxOTQxMjkyNiwicGF0aCI6Ii82NDc1NjAvMjQyNzU2NTEyLWI3YjZlNDZlLWQ0YjYtNGI1Zi05MGFmLWE4OTQwNDQwMTU3ZC5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwNjI2JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDYyNlQxNDQyMDZaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT0yNzI3YmUyYWZlZmJjMmVmZjZhNzhiNWNiNzRiNDYwMWMzNjE4ZjU4NTg0ZThiZTliYWVkMTFhZmUzMGZmYTZmJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.hwhdGADAjZ-d1z4JOEn_xC02XpoFxC6e5ZgOORIlBE4)
I'm not 100% what you mean about the GoObject
but I'll take a look at TypedArray
and see if I can see what you mean. Thanks for the quick reply!
We could also create a wrapper struct around the value we are using at the moment. It's a but more work but it would allow us to define our own implementation of fmt.Stringer
for that wrapper. Not as clean, but it could also work.
from goja.
What you see is an internal field on a Go struct, it's not the same as the instance property of a javascript object. So, if you call new URL("...").toString()
the prototype method should now be called. However, if you call ""+url
, it won't be. I'll make some adjustments.
from goja.
That makes sense, for now I managed to get it working. I was using console.log(myVar);
and it wasn't using toString
but explicitly calling did do the right thing. This unblocks me for now at least! Thanks again for the feedback!
If you find a better solution, let me know.
from goja.
I've changed it so that the methods are no longer defined on the wrapped values, so no need to delete them anymore, it should just work.
from goja.
That's great! Thanks for the quick turn around!
from goja.
Related Issues (20)
- AST Walk / Visitor pattern HOT 1
- Does goja support WebAssembly? or somehow run bytecode on goja? HOT 1
- Is the Web Platform in scope? HOT 1
- How to define async function in Goja HOT 4
- Subscribe *Promise Pending Status HOT 2
- How to Throw an Error From the Golang Function
- What Is Types Marshaling Among JS and GOLANG Spaces.
- Support for setting a Promise as handled to true HOT 3
- how to impove performance for a amount of constant literal in a javascript file HOT 4
- vm reuse problem HOT 2
- Request for Assistance: Obtaining Abstract Syntax Tree (AST) with Goja HOT 1
- How to limit the number of for loops HOT 1
- Fibers/Coroutines HOT 4
- Builtin Date locale's methods ignore locale argument HOT 1
- Export JS array to Go slice HOT 2
- if i want to let users do js on my server like cloudflare workers, is it possible to use your repo to do so? HOT 1
- abnormal string input to js function HOT 1
- Go binding for function rest params does not work as expected HOT 2
- how to register custom object into goja like before? HOT 3
- Listing class properties HOT 5
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 goja.