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

Extend Value into the Object type #74

Merged
merged 12 commits into from
Feb 8, 2021
Merged

Extend Value into the Object type #74

merged 12 commits into from
Feb 8, 2021

Conversation

rogchap
Copy link
Owner

@rogchap rogchap commented Feb 4, 2021

Basic casting of a Value to an Object (val.AsObject()) as well as converting Value to an Object (val.Object())

Only covers a small subset of the Object API as described here: https://v8.github.io/api/head/classv8_1_1Object.html

Also includes converting an ObjectTemplate to an Object with a given Context, and retrieving the Global Object from a Context.

This allows for things like this at runtime:

iso, _ := v8go.NewIsolate()
obj, _ := v8go.NewObjectTemplate(iso)
ctx, _ := v8go.NewContext(iso, obj)
global := ctx.Global()
console, _ := v8go.NewObjectTemplate(iso)
logfn, _ := v8go.NewFunctionTemplate(iso, func(info *v8go.FunctionCallbackInfo) *v8go.Value {
	fmt.Println(info.Args()[0])
	return nil
})
console.Set("log", logfn)
consoleObj, _ := console.NewInstance(ctx)
global.Set("console", consoleObj)
ctx.RunScript("console.log('foo')", "")
// Output:
// foo

@codecov
Copy link

codecov bot commented Feb 4, 2021

Codecov Report

Merging #74 (c8e550a) into master (85aad81) will increase coverage by 1.42%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #74      +/-   ##
==========================================
+ Coverage   92.44%   93.86%   +1.42%     
==========================================
  Files           9       10       +1     
  Lines         344      391      +47     
==========================================
+ Hits          318      367      +49     
+ Misses         19       17       -2     
  Partials        7        7              
Impacted Files Coverage Δ
context.go 93.05% <100.00%> (+0.40%) ⬆️
json.go 100.00% <100.00%> (ø)
object.go 100.00% <100.00%> (ø)
object_template.go 100.00% <100.00%> (ø)
value.go 95.03% <100.00%> (+1.48%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 85aad81...c8e550a. Read the comment docs.

@zwang
Copy link
Collaborator

zwang commented Feb 4, 2021

Nice, will this changes allow getting any object down the object tree and also override to set any object? Probably except overriding Global Object?

@rogchap
Copy link
Owner Author

rogchap commented Feb 4, 2021

Yes, you should be able to get and set any Object in the tree and the change would be reflected in the VM.
You can change the Global Object too; but do so at your own peril, as you could break the VM.

@rogchap rogchap marked this pull request as ready for review February 8, 2021 00:55
@rogchap rogchap requested a review from tmc February 8, 2021 00:55
Copy link
Collaborator

@tmc tmc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only minor comments. LGTM

json.go Outdated Show resolved Hide resolved
object_template_test.go Outdated Show resolved Hide resolved
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

Successfully merging this pull request may close these issues.

[feature] How to get the Promise result value? Support for the Object value type
3 participants