forked from rogchap/v8go
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add functions to separate compilation from running to speed up start …
…times on new isolates loading the same scripts. (rogchap#206) * CompileScript + RunCompiledScript Co-authored-by: Dylan Thacker-Smith <dylan.smith@shopify.com> * Use cached data pointer instead of copying data to a new byte slice - Compile options support - Add Bytes() on cached data, lazy load * Revert "Use cached data pointer instead of copying data to a new byte slice" This reverts commit 45a127e. * RtnCachedData to handle error and accept option arg * RunScript accepts cached data, CompileAndRun does not - updated changelog - updated readme * Introduce UnboundScript - Keep RunScript api * Input rejected value can be ignored * Fix warnnings around init * Delete wrapping cached data obj instead of just internal ptr * panic if Option and CachedData are used together in CompileOptions * Use global variables to share C++ enum values with Go * Rename CompilerOptions Option field to Mode * Rename ScriptCompilerCachedData to CompilerCachedData For brevity and because we aren't really exposing the concept of a ScriptCompiler and there currently isn't another public compiler type in the V8 API. Co-authored-by: Dylan Thacker-Smith <dylan.smith@shopify.com>
- Loading branch information
1 parent
7df088a
commit 4a6bcfb
Showing
10 changed files
with
423 additions
and
14 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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,20 @@ | ||
// Copyright 2021 the v8go contributors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
package v8go | ||
|
||
// #include "v8go.h" | ||
import "C" | ||
|
||
type CompileMode C.int | ||
|
||
var ( | ||
CompileModeDefault = CompileMode(C.ScriptCompilerNoCompileOptions) | ||
CompileModeEager = CompileMode(C.ScriptCompilerEagerCompile) | ||
) | ||
|
||
type CompilerCachedData struct { | ||
Bytes []byte | ||
Rejected bool | ||
} |
This file contains 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,39 @@ | ||
// Copyright 2021 the v8go contributors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
package v8go | ||
|
||
// #include <stdlib.h> | ||
// #include "v8go.h" | ||
import "C" | ||
import "unsafe" | ||
|
||
type UnboundScript struct { | ||
ptr C.UnboundScriptPtr | ||
iso *Isolate | ||
} | ||
|
||
// Run will bind the unbound script to the provided context and run it. | ||
// If the context provided does not belong to the same isolate that the script | ||
// was compiled in, Run will panic. | ||
// If an error occurs, it will be of type `JSError`. | ||
func (u *UnboundScript) Run(ctx *Context) (*Value, error) { | ||
if ctx.Isolate() != u.iso { | ||
panic("attempted to run unbound script in a context that belongs to a different isolate") | ||
} | ||
rtn := C.UnboundScriptRun(ctx.ptr, u.ptr) | ||
return valueResult(ctx, rtn) | ||
} | ||
|
||
// Create a code cache from the unbound script. | ||
func (u *UnboundScript) CreateCodeCache() *CompilerCachedData { | ||
rtn := C.UnboundScriptCreateCodeCache(u.iso.ptr, u.ptr) | ||
|
||
cachedData := &CompilerCachedData{ | ||
Bytes: []byte(C.GoBytes(unsafe.Pointer(rtn.data), rtn.length)), | ||
Rejected: int(rtn.rejected) == 1, | ||
} | ||
C.ScriptCompilerCachedDataDelete(rtn) | ||
return cachedData | ||
} |
This file contains 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,47 @@ | ||
// Copyright 2021 the v8go contributors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
package v8go_test | ||
|
||
import ( | ||
"testing" | ||
|
||
v8 "rogchap.com/v8go" | ||
) | ||
|
||
func TestUnboundScriptRun_OnlyInTheSameIsolate(t *testing.T) { | ||
str := "function foo() { return 'bar'; }; foo()" | ||
i1 := v8.NewIsolate() | ||
defer i1.Dispose() | ||
|
||
us, err := i1.CompileUnboundScript(str, "script.js", v8.CompileOptions{}) | ||
fatalIf(t, err) | ||
|
||
c1 := v8.NewContext(i1) | ||
defer c1.Close() | ||
|
||
val, err := us.Run(c1) | ||
fatalIf(t, err) | ||
if val.String() != "bar" { | ||
t.Fatalf("invalid value returned, expected bar got %v", val) | ||
} | ||
|
||
c2 := v8.NewContext(i1) | ||
defer c2.Close() | ||
|
||
val, err = us.Run(c2) | ||
fatalIf(t, err) | ||
if val.String() != "bar" { | ||
t.Fatalf("invalid value returned, expected bar got %v", val) | ||
} | ||
|
||
i2 := v8.NewIsolate() | ||
defer i2.Dispose() | ||
i2c1 := v8.NewContext(i2) | ||
defer i2c1.Close() | ||
|
||
if recoverPanic(func() { us.Run(i2c1) }) == nil { | ||
t.Error("expected panic running unbound script in a context belonging to a different isolate") | ||
} | ||
} |
Oops, something went wrong.