Skip to content

Commit

Permalink
Allow setting/unsetting of V8 feature flags (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
rogchap authored Mar 23, 2021
1 parent a487634 commit 78eaa3d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
6 changes: 5 additions & 1 deletion v8go.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1148,9 +1148,13 @@ ValuePtr ExceptionTypeError(IsolatePtr iso_ptr, const char* message) {
return static_cast<ValuePtr>(val);
}

/********** Version **********/
/********** v8::V8 **********/

const char* Version() {
return V8::GetVersion();
}

void SetFlags(const char* flags) {
V8::SetFlagsFromString(flags);
}
}
15 changes: 15 additions & 0 deletions v8go.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,24 @@ Package v8go provides an API to execute JavaScript.
package v8go

// #include "v8go.h"
// #include <stdlib.h>
import "C"
import (
"strings"
"unsafe"
)

// Version returns the version of the V8 Engine with the -v8go suffix
func Version() string {
return C.GoString(C.Version())
}

// SetFlags sets flags for V8. For possible flags: https://github.com/v8/v8/blob/master/src/flags/flag-definitions.h
// Flags are expected to be prefixed with `--`, for example: `--harmony`.
// Flags can be reverted using the `--no` prefix equivalent, for example: `--use_strict` vs `--nouse_strict`.
// Flags will affect all Isolates created, even after creation.
func SetFlags(flags ...string) {
cflags := C.CString(strings.Join(flags, " "))
C.SetFlags(cflags)
C.free(unsafe.Pointer(cflags))
}
1 change: 1 addition & 0 deletions v8go.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ extern ValuePtr ExceptionSyntaxError(IsolatePtr iso_ptr, const char* message);
extern ValuePtr ExceptionTypeError(IsolatePtr iso_ptr, const char* message);

const char* Version();
extern void SetFlags(const char* flags);

#ifdef __cplusplus
} // extern "C"
Expand Down
16 changes: 16 additions & 0 deletions v8go_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,19 @@ func TestVersion(t *testing.T) {
t.Errorf("version string is in the incorrect format: %s", v)
}
}

func TestSetFlag(t *testing.T) {
t.Parallel()
ctx, _ := v8go.NewContext()
if _, err := ctx.RunScript("a = 1", "default.js"); err != nil {
t.Errorf("expected <nil> error, but got: %v", err)
}
v8go.SetFlags("--use_strict")
if _, err := ctx.RunScript("b = 1", "use_strict.js"); err == nil {
t.Error("expected error but got <nil>")
}
v8go.SetFlags("--nouse_strict")
if _, err := ctx.RunScript("c = 1", "nouse_strict.js"); err != nil {
t.Errorf("expected <nil> error, but got: %v", err)
}
}

0 comments on commit 78eaa3d

Please sign in to comment.