forked from rogchap/v8go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
v8go.go
40 lines (35 loc) · 1.13 KB
/
v8go.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Copyright 2019 Roger Chapman and 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 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))
}
func initializeIfNecessary() {
v8once.Do(func() {
cflags := C.CString("--no-freeze_flags_after_init")
defer C.free(unsafe.Pointer(cflags))
C.SetFlags(cflags)
C.Init()
})
}