-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
74 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,36 @@ | ||
package extension | ||
|
||
import ( | ||
"fmt" | ||
"github.com/dop251/goja" | ||
"github.com/dop251/goja_nodejs/eventloop" | ||
"github.com/dop251/goja_nodejs/url" | ||
"github.com/monkeyWie/gopeed/pkg/download/extension/polyfill/fetch" | ||
"os" | ||
) | ||
|
||
func main() { | ||
file, err := os.ReadFile("D:\\code\\study\\node\\gopeed-extension-test\\dist\\index.js") | ||
if err != nil { | ||
panic(err) | ||
} | ||
SCRIPT := string(file) | ||
type Engine struct { | ||
loop *eventloop.EventLoop | ||
} | ||
|
||
loop := eventloop.NewEventLoop() | ||
loop.Run(func(vm *goja.Runtime) { | ||
func (e *Engine) Run(script string) (err error) { | ||
e.loop.Run(func(vm *goja.Runtime) { | ||
url.Enable(vm) | ||
if err := fetch.Enable(vm); err != nil { | ||
panic(err) | ||
if err = fetch.Enable(vm); err != nil { | ||
return | ||
} | ||
SCRIPT = ` | ||
(async function(){ | ||
const resp = await fetch('https://www.baidu.com') | ||
console.log(resp.text()) | ||
})() | ||
` | ||
_, err2 := vm.RunString(SCRIPT) | ||
if err2 != nil { | ||
fmt.Println(err2.Error()) | ||
} | ||
//vm.RunProgram(prg) | ||
_, err = vm.RunString(script) | ||
return | ||
}) | ||
return | ||
} | ||
|
||
func NewEngine() *Engine { | ||
loop := eventloop.NewEventLoop() | ||
return &Engine{ | ||
loop: loop, | ||
} | ||
} | ||
|
||
func Run(script string) (err error) { | ||
engine := NewEngine() | ||
return engine.Run(script) | ||
} |
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,38 @@ | ||
package extension | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestRun(t *testing.T) { | ||
type args struct { | ||
script string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "ok", | ||
args: args{ | ||
script: "1+1", | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "error", | ||
args: args{ | ||
script: "a", | ||
}, | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if err := Run(tt.args.script); (err != nil) != tt.wantErr { | ||
t.Errorf("Run() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
}) | ||
} | ||
} |
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