Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Freezing GO struct to consume by javascript modules #517

Open
stockiNail opened this issue Mar 7, 2024 · 1 comment
Open

Freezing GO struct to consume by javascript modules #517

stockiNail opened this issue Mar 7, 2024 · 1 comment

Comments

@stockiNail
Copy link

I'm addressing a use case where we need to pass to javascript module GO structs, to consume in read-only mode.

Otto is enabling in my app a pluggable approach where whoever can develop small pieces of code which will be triggered by event or by whatever else cause.
The javascript plugin should consume data which are passed to it by functions, leverage on Otto implementation, but it shouldn't be able to change the struct, therefore it should only read the data.

I have tried to invoke Object.freeze against the GO structs (passed as argument or passed by Otto.toValue) but it doesn't work.
It seems that the GO structs passed to javascript are not 100% javascript object and some javascript Object functions don't work against them.
I have tried also to define writable and configurable flags (and extensible on the objects) on every single property (by defineProperty and using the descriptor) but it doesn't work.

Going a bit in deep in Otto source code, I see the following:

otto/runtime.go

Lines 666 to 670 in b755419

case Object, *Object, object, *object:
// Nothing happens.
// FIXME We should really figure out what can come here.
// This catch-all is ugly.
default:

which probably could address this use case when implemented.

I know that a possible solution could be to clone the structs before passing them to javascript but this is very resource consuming and I would avoid it.

Maybe there is a trick that I haven't discoreved yet... ;)

@stevenh
Copy link
Collaborator

stevenh commented Sep 9, 2024

Yer that's quite specific case, and not something that go supports natively, that's not to say it couldn't be possible in the JS side.

Happy to take a PR to address this case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants