diff --git a/nf/nvm/cfuncs.go b/nf/nvm/cfuncs.go
index 1c3993b47..b9b5bc8ad 100644
--- a/nf/nvm/cfuncs.go
+++ b/nf/nvm/cfuncs.go
@@ -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);
diff --git a/nf/nvm/engine_v8.go b/nf/nvm/engine_v8.go
index 1012136c8..09fbd9899 100644
--- a/nf/nvm/engine_v8.go
+++ b/nf/nvm/engine_v8.go
@@ -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);
@@ -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)))
}
diff --git a/nf/nvm/engine_v8_test.go b/nf/nvm/engine_v8_test.go
index 88a6d42be..805747df7 100644
--- a/nf/nvm/engine_v8_test.go
+++ b/nf/nvm/engine_v8_test.go
@@ -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},
diff --git a/nf/nvm/logfunc.go b/nf/nvm/logger.go
similarity index 81%
rename from nf/nvm/logfunc.go
rename to nf/nvm/logger.go
index 124d228e9..b7287f48a 100644
--- a/nf/nvm/logfunc.go
+++ b/nf/nvm/logger.go
@@ -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)
}
}
diff --git a/nf/nvm/native-lib/libv8engine.dylib b/nf/nvm/native-lib/libv8engine.dylib
index 728e6df2c..7b106c5f9 100755
Binary files a/nf/nvm/native-lib/libv8engine.dylib and b/nf/nvm/native-lib/libv8engine.dylib differ
diff --git a/nf/nvm/test/test_require.js b/nf/nvm/test/test_require.js
new file mode 100644
index 000000000..ed5aab2f7
--- /dev/null
+++ b/nf/nvm/test/test_require.js
@@ -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 .
+//
+'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;
+ }
+}
diff --git a/nf/nvm/v8/engine.cc b/nf/nvm/v8/engine.cc
index 22cd6afee..4663550fe 100644
--- a/nf/nvm/v8/engine.cc
+++ b/nf/nvm/v8/engine.cc
@@ -90,7 +90,7 @@ int RunScriptSource(V8Engine *e, const char *data, void *lcsHandler,
// Create global object template.
Local 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));
diff --git a/nf/nvm/v8/lib/execution_env.cc b/nf/nvm/v8/lib/execution_env.cc
index 9d321155b..93a09acbe 100644
--- a/nf/nvm/v8/lib/execution_env.cc
+++ b/nf/nvm/v8/lib/execution_env.cc
@@ -21,10 +21,20 @@
#include "log_callback.h"
int SetupExecutionEnv(Isolate *isolate, Local &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 source =
String::NewFromUtf8(isolate, data, NewStringType::kNormal)