This repository has been archived by the owner on Oct 12, 2023. It is now read-only.
forked from rogchap/v8go
-
Notifications
You must be signed in to change notification settings - Fork 3
Heap snapshots #39
Open
GustavoCaso
wants to merge
1
commit into
master
Choose a base branch
from
heap-snapshots
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Heap snapshots #39
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,104 @@ | ||
// 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 ( | ||
"errors" | ||
"unsafe" | ||
) | ||
|
||
type FunctionCodeHandling int | ||
|
||
const ( | ||
FunctionCodeHandlingKlear FunctionCodeHandling = iota | ||
FunctionCodeHandlingKeep | ||
) | ||
|
||
type StartupData struct { | ||
data []byte | ||
raw_size C.int | ||
} | ||
|
||
type SnapshotCreator struct { | ||
ptr C.SnapshotCreatorPtr | ||
iso *Isolate | ||
defaultContextAdded bool | ||
} | ||
|
||
func NewSnapshotCreator() *SnapshotCreator { | ||
v8once.Do(func() { | ||
C.Init() | ||
}) | ||
GustavoCaso marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
rtn := C.NewSnapshotCreator() | ||
|
||
return &SnapshotCreator{ | ||
ptr: rtn.creator, | ||
iso: &Isolate{ptr: rtn.iso}, | ||
defaultContextAdded: false, | ||
} | ||
} | ||
|
||
func (s *SnapshotCreator) GetIsolate() (*Isolate, error) { | ||
if s.ptr == nil { | ||
return nil, errors.New("v8go: Cannot get Isolate after creating the blob") | ||
} | ||
|
||
return s.iso, nil | ||
} | ||
|
||
func (s *SnapshotCreator) SetDeafultContext(ctx *Context) error { | ||
if s.defaultContextAdded { | ||
return errors.New("v8go: Cannot set multiple default context for snapshot creator") | ||
} | ||
|
||
C.SetDefaultContext(s.ptr, ctx.ptr) | ||
s.defaultContextAdded = true | ||
ctx.ptr = nil | ||
|
||
return nil | ||
} | ||
|
||
func (s *SnapshotCreator) AddContext(ctx *Context) (int, error) { | ||
if s.ptr == nil { | ||
return 0, errors.New("v8go: Cannot add context to snapshot creator after creating the blob") | ||
} | ||
|
||
index := C.AddContext(s.ptr, ctx.ptr) | ||
ctx.ptr = nil | ||
|
||
return int(index), nil | ||
} | ||
|
||
func (s *SnapshotCreator) Create(functionCode FunctionCodeHandling) (*StartupData, error) { | ||
if s.ptr == nil { | ||
return nil, errors.New("v8go: Cannot use snapshot creator after creating the blob") | ||
} | ||
|
||
if !s.defaultContextAdded { | ||
return nil, errors.New("v8go: Cannot create a snapshot without a default context") | ||
} | ||
|
||
rtn := C.CreateBlob(s.ptr, C.int(functionCode)) | ||
|
||
s.ptr = nil | ||
s.iso.ptr = nil | ||
|
||
raw_size := rtn.raw_size | ||
data := C.GoBytes(unsafe.Pointer(rtn.data), raw_size) | ||
|
||
C.SnapshotBlobDelete(rtn) | ||
|
||
return &StartupData{data: data, raw_size: raw_size}, nil | ||
} | ||
|
||
func (s *SnapshotCreator) Dispose() { | ||
if s.ptr != nil { | ||
C.DeleteSnapshotCreator(s.ptr) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should:
k
prefix as well as correctFunctionCodeHandlingKlear
->FunctionCodeHandlingClear