2015-07-07 03:54:22 +03:00
|
|
|
// Copyright 2015 The go-ethereum Authors
|
2015-07-22 19:48:40 +03:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-07 03:54:22 +03:00
|
|
|
//
|
2015-07-23 19:35:11 +03:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
2015-07-07 03:54:22 +03:00
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
2015-07-22 19:48:40 +03:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-07 03:54:22 +03:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 19:48:40 +03:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-07 03:54:22 +03:00
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
2015-07-22 19:48:40 +03:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-07 03:54:22 +03:00
|
|
|
|
2015-03-15 08:13:39 +02:00
|
|
|
package jsre
|
|
|
|
|
|
|
|
import (
|
2015-03-25 16:58:52 +02:00
|
|
|
"os"
|
2024-04-09 09:22:53 +03:00
|
|
|
"path/filepath"
|
2020-01-27 13:50:48 +03:00
|
|
|
"reflect"
|
2015-03-15 08:13:39 +02:00
|
|
|
"testing"
|
2015-04-22 03:31:59 +03:00
|
|
|
"time"
|
2015-05-25 03:27:37 +03:00
|
|
|
|
2020-01-27 13:50:48 +03:00
|
|
|
"github.com/dop251/goja"
|
2015-03-15 08:13:39 +02:00
|
|
|
)
|
|
|
|
|
2020-01-27 13:50:48 +03:00
|
|
|
type testNativeObjectBinding struct {
|
|
|
|
vm *goja.Runtime
|
|
|
|
}
|
2015-03-15 08:13:39 +02:00
|
|
|
|
|
|
|
type msg struct {
|
|
|
|
Msg string
|
|
|
|
}
|
|
|
|
|
2020-01-27 13:50:48 +03:00
|
|
|
func (no *testNativeObjectBinding) TestMethod(call goja.FunctionCall) goja.Value {
|
|
|
|
m := call.Argument(0).ToString().String()
|
|
|
|
return no.vm.ToValue(&msg{m})
|
2015-03-15 08:13:39 +02:00
|
|
|
}
|
|
|
|
|
2022-04-08 16:44:55 +03:00
|
|
|
func newWithTestJS(t *testing.T, testjs string) *JSRE {
|
|
|
|
dir := t.TempDir()
|
2015-08-06 17:19:09 +03:00
|
|
|
if testjs != "" {
|
2024-04-09 09:22:53 +03:00
|
|
|
if err := os.WriteFile(filepath.Join(dir, "test.js"), []byte(testjs), os.ModePerm); err != nil {
|
2015-08-06 17:19:09 +03:00
|
|
|
t.Fatal("cannot create test.js:", err)
|
|
|
|
}
|
|
|
|
}
|
2020-01-27 13:50:48 +03:00
|
|
|
jsre := New(dir, os.Stdout)
|
2022-04-08 16:44:55 +03:00
|
|
|
return jsre
|
2015-08-06 17:19:09 +03:00
|
|
|
}
|
|
|
|
|
2015-03-15 08:13:39 +02:00
|
|
|
func TestExec(t *testing.T) {
|
2024-09-16 09:44:47 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2022-04-08 16:44:55 +03:00
|
|
|
jsre := newWithTestJS(t, `msg = "testMsg"`)
|
2015-03-15 08:13:39 +02:00
|
|
|
|
|
|
|
err := jsre.Exec("test.js")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("expected no error, got %v", err)
|
|
|
|
}
|
|
|
|
val, err := jsre.Run("msg")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("expected no error, got %v", err)
|
|
|
|
}
|
2020-01-27 13:50:48 +03:00
|
|
|
if val.ExportType().Kind() != reflect.String {
|
2015-03-15 08:13:39 +02:00
|
|
|
t.Errorf("expected string value, got %v", val)
|
|
|
|
}
|
2015-03-15 13:42:17 +02:00
|
|
|
exp := "testMsg"
|
2020-01-27 13:50:48 +03:00
|
|
|
got := val.ToString().String()
|
2015-03-15 13:42:17 +02:00
|
|
|
if exp != got {
|
|
|
|
t.Errorf("expected '%v', got '%v'", exp, got)
|
2015-03-15 08:13:39 +02:00
|
|
|
}
|
2015-04-22 03:31:59 +03:00
|
|
|
jsre.Stop(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNatto(t *testing.T) {
|
2024-09-16 09:44:47 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2022-04-08 16:44:55 +03:00
|
|
|
jsre := newWithTestJS(t, `setTimeout(function(){msg = "testMsg"}, 1);`)
|
2015-04-22 03:31:59 +03:00
|
|
|
|
|
|
|
err := jsre.Exec("test.js")
|
|
|
|
if err != nil {
|
2022-01-21 14:22:44 +03:00
|
|
|
t.Fatalf("expected no error, got %v", err)
|
2015-04-22 03:31:59 +03:00
|
|
|
}
|
2015-11-05 12:36:10 +03:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2015-04-22 03:31:59 +03:00
|
|
|
val, err := jsre.Run("msg")
|
|
|
|
if err != nil {
|
2022-01-21 14:22:44 +03:00
|
|
|
t.Fatalf("expected no error, got %v", err)
|
2015-04-22 03:31:59 +03:00
|
|
|
}
|
2020-01-27 13:50:48 +03:00
|
|
|
if val.ExportType().Kind() != reflect.String {
|
2022-01-21 14:22:44 +03:00
|
|
|
t.Fatalf("expected string value, got %v", val)
|
2015-04-22 03:31:59 +03:00
|
|
|
}
|
|
|
|
exp := "testMsg"
|
2020-01-27 13:50:48 +03:00
|
|
|
got := val.ToString().String()
|
2015-04-22 03:31:59 +03:00
|
|
|
if exp != got {
|
2022-01-21 14:22:44 +03:00
|
|
|
t.Fatalf("expected '%v', got '%v'", exp, got)
|
2015-04-22 03:31:59 +03:00
|
|
|
}
|
|
|
|
jsre.Stop(false)
|
2015-03-15 08:13:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBind(t *testing.T) {
|
2024-09-16 09:44:47 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2016-05-06 12:40:23 +03:00
|
|
|
jsre := New("", os.Stdout)
|
2015-08-11 19:14:46 +03:00
|
|
|
defer jsre.Stop(false)
|
2015-03-15 08:13:39 +02:00
|
|
|
|
2020-01-27 13:50:48 +03:00
|
|
|
jsre.Set("no", &testNativeObjectBinding{vm: jsre.vm})
|
2015-03-15 08:13:39 +02:00
|
|
|
|
2015-08-11 19:14:46 +03:00
|
|
|
_, err := jsre.Run(`no.TestMethod("testMsg")`)
|
2015-03-15 08:13:39 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("expected no error, got %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-15 13:42:17 +02:00
|
|
|
func TestLoadScript(t *testing.T) {
|
2024-09-16 09:44:47 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2022-04-08 16:44:55 +03:00
|
|
|
jsre := newWithTestJS(t, `msg = "testMsg"`)
|
2015-03-15 08:13:39 +02:00
|
|
|
|
2015-03-15 13:42:17 +02:00
|
|
|
_, err := jsre.Run(`loadScript("test.js")`)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("expected no error, got %v", err)
|
2015-03-15 08:13:39 +02:00
|
|
|
}
|
2015-03-15 13:42:17 +02:00
|
|
|
val, err := jsre.Run("msg")
|
2015-03-15 08:13:39 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("expected no error, got %v", err)
|
|
|
|
}
|
2020-01-27 13:50:48 +03:00
|
|
|
if val.ExportType().Kind() != reflect.String {
|
2015-03-15 13:42:17 +02:00
|
|
|
t.Errorf("expected string value, got %v", val)
|
|
|
|
}
|
|
|
|
exp := "testMsg"
|
2020-01-27 13:50:48 +03:00
|
|
|
got := val.ToString().String()
|
2015-03-15 13:42:17 +02:00
|
|
|
if exp != got {
|
|
|
|
t.Errorf("expected '%v', got '%v'", exp, got)
|
|
|
|
}
|
2015-04-22 03:31:59 +03:00
|
|
|
jsre.Stop(false)
|
2015-08-11 19:14:46 +03:00
|
|
|
}
|