Skip to content

Commit

Permalink
v8: require now support lib cache, logFunc in V8Engine now rename to …
Browse files Browse the repository at this point in the history
…V8Log.
  • Loading branch information
Robin Zhong committed Nov 3, 2017
1 parent 2972162 commit ae37561
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 20 deletions.
6 changes: 3 additions & 3 deletions nf/nvm/cfuncs.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
package nvm

/*
void GoLogFunc(int level, const char *msg);
void V8Log(int level, const char *msg);
char *StorageGetFunc(void *handler, const char *key);
int StoragePutFunc(void *handler, const char *key, const char *value);
int StorageDelFunc(void *handler, const char *key);
// The gateway function
void GoLogFunc_cgo(int level, const char *msg) {
GoLogFunc(level, msg);
void V8Log_cgo(int level, const char *msg) {
V8Log(level, msg);
};
char *StorageGetFunc_cgo(void *handler, const char *key) {
return StorageGetFunc(handler, key);
Expand Down
4 changes: 2 additions & 2 deletions nf/nvm/engine_v8.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ package nvm
#include "v8/engine.h"
// Forward declaration.
void GoLogFunc_cgo(int level, const char *msg);
void V8Log_cgo(int level, const char *msg);
char *StorageGetFunc_cgo(void *handler, const char *key);
int StoragePutFunc_cgo(void *handler, const char *key, const char *value);
int StorageDelFunc_cgo(void *handler, const char *key);
Expand Down Expand Up @@ -69,7 +69,7 @@ type V8Engine struct {
// InitV8Engine initialize the v8 engine.
func InitV8Engine() {
C.Initialize()
C.InitializeLogger((C.LogFunc)(unsafe.Pointer(C.GoLogFunc_cgo)))
C.InitializeLogger((C.LogFunc)(unsafe.Pointer(C.V8Log_cgo)))
C.InitializeStorage((C.StorageGetFunc)(unsafe.Pointer(C.StorageGetFunc_cgo)), (C.StoragePutFunc)(unsafe.Pointer(C.StoragePutFunc_cgo)), (C.StorageDelFunc)(unsafe.Pointer(C.StorageDelFunc_cgo)))
}

Expand Down
11 changes: 11 additions & 0 deletions nf/nvm/engine_v8_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,30 @@
package nvm

import (
"flag"
"io/ioutil"
"os"
"testing"

"github.com/nebulasio/go-nebulas/common/trie"
"github.com/nebulasio/go-nebulas/storage"
"github.com/nebulasio/go-nebulas/util/logging"
"github.com/stretchr/testify/assert"
)

func TestMain(m *testing.M) {
logging.EnableFuncNameLogger()

flag.Parse()
os.Exit(m.Run())
}

func TestRunScriptSource(t *testing.T) {
tests := []struct {
filepath string
expectedErr error
}{
{"test/test_require.js", nil},
{"test/test_console.js", nil},
{"test/test_storage_handlers.js", nil},
{"test/test_storage_class.js", nil},
Expand Down
17 changes: 7 additions & 10 deletions nf/nvm/logfunc.go → nf/nvm/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,20 @@ import (
log "github.com/sirupsen/logrus"
)

//export GoLogFunc
func GoLogFunc(level int, msg *C.char) {
entry := log.WithFields(log.Fields{
"func": "GoLogFunc",
})
//export V8Log
func V8Log(level int, msg *C.char) {
s := C.GoString(msg)

switch level {
case 1:
entry.Debug(s)
log.Debug(s)
case 2:
entry.Warn(s)
log.Warn(s)
case 3:
entry.Info(s)
log.Info(s)
case 4:
entry.Error(s)
log.Error(s)
default:
entry.Error(s)
log.Error(s)
}
}
Binary file modified nf/nvm/native-lib/libv8engine.dylib
Binary file not shown.
33 changes: 33 additions & 0 deletions nf/nvm/test/test_require.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (C) 2017 go-nebulas authors
//
// This file is part of the go-nebulas library.
//
// the go-nebulas library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// the go-nebulas library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with the go-nebulas library. If not, see <http://www.gnu.org/licenses/>.
//
'use strict';

var console2 = require('console.js');
if (!Object.is(console, console2)) {
throw new Error("require should caches libs.");
}

var err = new Error("require should throw error when file does not exist.");
try {
require("not-exist-file");
throw err;
} catch (e) {
if (e === err) {
throw e;
}
}
2 changes: 1 addition & 1 deletion nf/nvm/v8/engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ int RunScriptSource(V8Engine *e, const char *data, void *lcsHandler,

// Create global object template.
Local<ObjectTemplate> globalTpl = ObjectTemplate::New(isolate);
globalTpl->Set(String::NewFromUtf8(isolate, "require"),
globalTpl->Set(String::NewFromUtf8(isolate, "_native_require"),
FunctionTemplate::New(isolate, requireCallback));
globalTpl->Set(String::NewFromUtf8(isolate, "_native_log"),
FunctionTemplate::New(isolate, logCallback));
Expand Down
18 changes: 14 additions & 4 deletions nf/nvm/v8/lib/execution_env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,20 @@
#include "log_callback.h"

int SetupExecutionEnv(Isolate *isolate, Local<Context> &context) {
char data[] = "const console = require('console.js');"
"const ContractStorage = require('storage.js');"
"const LocalContractStorage = ContractStorage.lcs;"
"const GlobalContractStorage = ContractStorage.gcs;";
char data[] =
"const require = (function() {"
" var requiredLibs = {};"
" return function(filename) {"
" if (!(filename in requiredLibs)) {"
" requiredLibs[filename] = _native_require(filename);"
" }"
" return requiredLibs[filename];"
" };"
"})();"
"const console = require('console.js');"
"const ContractStorage = require('storage.js');"
"const LocalContractStorage = ContractStorage.lcs;"
"const GlobalContractStorage = ContractStorage.gcs;";

Local<String> source =
String::NewFromUtf8(isolate, data, NewStringType::kNormal)
Expand Down

0 comments on commit ae37561

Please sign in to comment.