forked from rogchap/v8go
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Isolate creation and disposal for Yao (dev)
- Loading branch information
Showing
5 changed files
with
222 additions
and
43 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package v8go | ||
|
||
import ( | ||
"fmt" | ||
"unsafe" | ||
) | ||
|
||
// #include "v8go.h" | ||
// #include <stdlib.h> | ||
import "C" | ||
|
||
// YaoNewIsolate creates a new V8 isolate. Only one thread may access | ||
// a given isolate at a time, but different threads may access | ||
// different isolates simultaneously. | ||
// When an isolate is no longer used its resources should be freed | ||
// by calling iso.Dispose(). | ||
// An *Isolate can be used as a v8go.ContextOption to create a new | ||
// Context, rather than creating a new default Isolate. | ||
func YaoNewIsolate() *Isolate { | ||
iso := &Isolate{ | ||
ptr: C.YaoNewIsolate(), | ||
cbs: make(map[int]FunctionCallback), | ||
} | ||
iso.null = newValueNull(iso) | ||
iso.undefined = newValueUndefined(iso) | ||
return iso | ||
} | ||
|
||
// YaoNewIsolateFromGlobal creates a new V8 isolate from global. | ||
func YaoNewIsolateFromGlobal() (*Isolate, error) { | ||
|
||
ptr := C.YaoNewIsolateFromGlobal() | ||
if ptr == nil { | ||
return nil, fmt.Errorf("YaoNewIsolateFromGlobal failed") | ||
} | ||
|
||
iso := &Isolate{ | ||
ptr: ptr, | ||
cbs: make(map[int]FunctionCallback), | ||
} | ||
|
||
return iso, nil | ||
} | ||
|
||
// YaoDispose will dispose the Isolate VM; subsequent calls will panic. | ||
func YaoDispose() { | ||
C.YaoDispose() | ||
} | ||
|
||
// Copy copies the current isolate. | ||
func (iso *Isolate) Copy() (*Isolate, error) { | ||
if iso == nil || iso.ptr == nil { | ||
return nil, fmt.Errorf("invalid isolate") | ||
} | ||
new := &Isolate{ | ||
ptr: C.YaoCopyIsolate(iso.ptr), | ||
cbs: make(map[int]FunctionCallback), | ||
} | ||
return new, nil | ||
} | ||
|
||
// AsGlobal makes the isolate into a global object. | ||
func (iso *Isolate) AsGlobal() { | ||
C.YaoIsolateAsGlobal(iso.ptr) | ||
} | ||
|
||
// YaoInit initializes V8 with the given heap size limit. | ||
func YaoInit(heapSizeLimitMB uint) { | ||
v8once.Do(func() { | ||
cflags := C.CString("--no-freeze_flags_after_init") | ||
if heapSizeLimitMB > 0 { | ||
cflags = C.CString(fmt.Sprintf("--no-freeze_flags_after_init --max_old_space_size=%d", heapSizeLimitMB)) | ||
} | ||
defer C.free(unsafe.Pointer(cflags)) | ||
C.SetFlags(cflags) | ||
C.Init() | ||
}) | ||
} |
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,73 @@ | ||
package v8go_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
v8 "rogchap.com/v8go" | ||
) | ||
|
||
func TestYaoVersionPrint(t *testing.T) { | ||
v := v8.Version() | ||
fmt.Println("Version", v) | ||
} | ||
|
||
func TestYaoInit(t *testing.T) { | ||
t.Parallel() | ||
v8.YaoInit(1024) | ||
defer v8.YaoDispose() | ||
|
||
iso := v8.YaoNewIsolate() | ||
defer iso.Dispose() | ||
|
||
// stat := iso.GetHeapStatistics() | ||
// if stat.HeapSizeLimit > 1100*1024*1024 { | ||
// t.Errorf("HeapSizeLimit error %d", stat.HeapSizeLimit) | ||
// } | ||
} | ||
|
||
func TestYaoCopyIsolate(t *testing.T) { | ||
t.Parallel() | ||
v8.YaoInit(1024) | ||
defer v8.YaoDispose() | ||
|
||
iso := v8.YaoNewIsolate() | ||
defer iso.Dispose() | ||
|
||
new, err := iso.Copy() | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
defer new.Dispose() | ||
} | ||
|
||
func TestYaoIsolateAsGlobal(t *testing.T) { | ||
|
||
v8.YaoInit(1024) | ||
defer v8.YaoDispose() | ||
|
||
iso := v8.YaoNewIsolate() | ||
defer iso.Dispose() | ||
|
||
iso.AsGlobal() | ||
} | ||
|
||
func TestYaoNewIsolateFromGlobal(t *testing.T) { | ||
v8.YaoInit(1024) | ||
defer v8.YaoDispose() | ||
|
||
iso := v8.YaoNewIsolate() | ||
iso.AsGlobal() | ||
iso.Dispose() | ||
iso = nil | ||
|
||
new, err := v8.YaoNewIsolateFromGlobal() | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
if new == nil { | ||
t.Errorf("new is nil") | ||
} | ||
defer new.Dispose() | ||
} |