forked from rogchap/v8go
-
-
Notifications
You must be signed in to change notification settings - Fork 17
Add support for "external" values #107
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
Open
stroiman
wants to merge
3
commits into
tommie:master
Choose a base branch
from
stroiman:support-for-embedded-objects
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| package v8go_test | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "runtime/cgo" | ||
|
|
||
| v8 "github.com/tommie/v8go" | ||
| ) | ||
|
|
||
| type Calculator struct { | ||
| stack []int32 | ||
| } | ||
|
|
||
| func (c *Calculator) Peek() int32 { | ||
| l := len(c.stack) | ||
| if l > 0 { | ||
| return c.stack[l-1] | ||
| } else { | ||
| return 0 | ||
| } | ||
| } | ||
| func (c *Calculator) Push(v int32) { c.stack = append(c.stack, v) } | ||
| func (c *Calculator) Add() { | ||
| l := len(c.stack) | ||
| if l < 2 { | ||
| panic("Not enough elements") | ||
| } | ||
| a := c.stack[l-1] | ||
| b := c.stack[l-2] | ||
| c.stack[l-2] = a + b | ||
| c.stack = c.stack[0 : l-1] | ||
| } | ||
|
|
||
| // getInstance retrieves the go Calculator instance that is stored in an | ||
| // _internal field_. | ||
| func getInstance(info *v8.FunctionCallbackInfo) *Calculator { | ||
| var internalField *v8.Value = info.This().GetInternalField(0) | ||
| var handle cgo.Handle = internalField.ExternalHandle() | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This handle needs to be deleted! Specifically as this serves as a public example - it should be clear that handles need releasing. |
||
| calculator, ok := handle.Value().(*Calculator) | ||
| if !ok { | ||
| panic("Not a calculator") | ||
| } | ||
| return calculator | ||
| } | ||
|
|
||
| func CreateJSCalculator(iso *v8.Isolate) *v8.FunctionTemplate { | ||
| constructor := v8.NewFunctionTemplate(iso, func(info *v8.FunctionCallbackInfo) *v8.Value { | ||
| calculator := &Calculator{} | ||
| handle := cgo.NewHandle(calculator) | ||
| info.This().SetInternalField(0, | ||
| v8.NewValueExternalHandle(iso, handle)) | ||
| return nil | ||
| }) | ||
|
|
||
| // Set the internal field count on the **instance template**. | ||
| instanceTemplate := constructor.InstanceTemplate() | ||
| instanceTemplate.SetInternalFieldCount(1) | ||
|
|
||
| // Create the methods on the **prototype template**. | ||
| prototypeTemplate := constructor.PrototypeTemplate() | ||
| prototypeTemplate.Set("push", | ||
| v8.NewFunctionTemplate(iso, | ||
| func(info *v8.FunctionCallbackInfo) *v8.Value { | ||
| calculator := getInstance(info) | ||
| calculator.Push(info.Args()[0].Int32()) | ||
| return nil | ||
| })) | ||
| prototypeTemplate.Set("add", | ||
| v8.NewFunctionTemplate(iso, | ||
| func(info *v8.FunctionCallbackInfo) *v8.Value { | ||
| calculator := getInstance(info) | ||
| calculator.Add() | ||
| return nil | ||
| })) | ||
| prototypeTemplate.Set("peek", | ||
| v8.NewFunctionTemplateWithError(iso, | ||
| func(info *v8.FunctionCallbackInfo) (*v8.Value, error) { | ||
| calculator := getInstance(info) | ||
| return v8.NewValue(iso, calculator.Peek()) | ||
| })) | ||
| return constructor | ||
| } | ||
|
|
||
| func ExampleNewValueExternalHandle() { | ||
| iso := v8.NewIsolate() | ||
| defer iso.Dispose() | ||
|
|
||
| global := v8.NewObjectTemplate(iso) | ||
| global.Set("Calculator", CreateJSCalculator(iso)) | ||
| ctx := v8.NewContext(iso, global) | ||
|
|
||
| defer ctx.Close() | ||
| v, err := ctx.RunScript(` | ||
| const calculator = new Calculator() | ||
| calculator.push(7) | ||
| calculator.push(35) | ||
| calculator.add() | ||
| calculator.peek()`, "") | ||
| if err != nil { | ||
| fmt.Println("Error running script", err.Error()) | ||
| return | ||
| } | ||
| fmt.Printf("The result is: %d\n", v.Integer()) | ||
| // Output: | ||
| // The result is: 42 | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest moving this into
example/(and skipping theexample_prefix). The root is getting pretty large.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree on the "pretty large" - but does it work the same? Is the example embedded in the documentation the same way?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, right. No, that won't work.
This example feels large enough that it might make sense to keep it separate anyway. If it could be made into 30 lines, I think having it next to the function is reasonable (and then we wouldn't have one file per example case).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The "one file per example" is a special case where the file is included in its entirety which I chose for this as the
Calculatoris a crucial part of the example - it's about how to create a JS wrapper object on top of a native type - a crucial part for my own use case.Of course, a different example could be created using an internal go type, e.g., create a stack, where the value is a
*[]v8.Valueor something - then the example function could stand on it's own. The calculator was just the use case I came up with when writing the example.The name of the example function dictates where in the docs it's included - right now it's associated with
NewValueExternalHandle- but maybe it shouldn't be assiciated with this function specifically, as the use case requires the use ofSet/GetInternalValueandInternalValueCount- and how to create the values in the constructor.E.g., this is from my own godoc server running
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Btw, looking at the example, I notice something that should probably be handled differently
If the field can't be looked up, it should return a
TypeError(I guess that's the appropriate return value). An example JS code that would be affectedAs it's setting the prototype, it would call the go callback, but as this wasn't created through the constructor, it wouldn't have the internal field. I experienced a similar bug in my own code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
E.g., in Firefox:

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I think the example is too long for being tied to a single function. It feels unfocused, even if the example is good. Perhaps flipping it so the relevant function invocation is first would help shake that feeling.