-
Notifications
You must be signed in to change notification settings - Fork 1
/
engine_es_test.go
237 lines (226 loc) · 5 KB
/
engine_es_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package engine
import (
"context"
"github.com/ZenLiuCN/fn"
"os"
"testing"
"time"
)
func TestSimple(t *testing.T) {
e := NewEngine()
println(IsNullish(fn.Panic1(e.RunJs("console.log('123'+'2324')"))))
println(IsNullish(fn.Panic1(e.RunTs(`
const adder=(i:number)=>i+1
const added=(u:number)=>{let x=u;return (i:number)=>x+i}
console.log(adder(1234))
const add=added(5)
console.log(add(1))
console.log(add(2))
console.log(add(3))
`))))
}
func TestEs2015(t *testing.T) {
e := NewEngine()
println(IsNullish(fn.Panic1(e.RunString(
//language=javascript
`
class A {
constructor(age){
this.age=age
}
}
console.log(typeof A)
const x=1
console.log(Number.EPSILON)
console.log(Number.MIN_SAFE_INTEGER)
console.log(Number.MAX_SAFE_INTEGER)
console.log(Math.trunc(1.2))
console.log(Math.sign(1.2))
console.log({[x+"b"]:1})
console.log(new A(1))
`))))
e.Await()
}
func TestEs2016(t *testing.T) {
e := NewEngine()
println(IsNullish(fn.Panic1(e.RunString(
//language=javascript
`
console.log(2**12)
console.log([1,2].includes(1))
`))))
}
func TestEs2017(t *testing.T) {
e := NewEngine()
println(IsNullish(fn.Panic1(e.RunString(
//language=javascript
`
const b=()=>new Promise((r,j)=>r(1))
const c= async ()=> await b()
console.log(typeof b)
console.log(b.name)
c().then(v=>console.log('future',v))
// console.log(await c()) TOP Level not supported yet
console.log(Object.values({a:1,b:2}))
console.log(Object.entries({a:1,b:2}))
console.log(Object.getOwnPropertyDescriptors({a:1,b:2}))
const str = 'adata'
console.log(str.padStart(10))
console.log(str.padEnd(10))
`))))
e.Await()
}
func TestEs2018(t *testing.T) {
e := NewEngine()
println(IsNullish(fn.Panic1(e.RunString(
//language=javascript
`
/*const asyncIterable = {
[Symbol.asyncIterator]() {
return {
i: 0,
next() {
if (this.i < 3) {
return Promise.resolve({ value: this.i++, done: false })
}
return Promise.resolve({ done: true })
}
}
}
}
(async function () {
for await (num of asyncIterable) {
console.log(num)
}
})()*/
console.log("asyncIterable not supported yet")
const n = { name: 'som' }
const a = { age: 182 }
const person = { ...n, ...a }
console.log(person)
const syn=()=>new Promise((r,j)=>r(1))
syn()
.then(v=>console.log(v))
.finally(()=>{
console.log("finally will never end")
return new Promise((r,j)=>r())
})
`))))
ctx, cc := context.WithTimeout(context.Background(), time.Second)
defer cc()
e.AwaitWithContext(ctx)
}
func TestEs2019(t *testing.T) {
e := NewEngine()
println(IsNullish(fn.Panic1(e.RunString(
//language=javascript
`
console.log([1,2,3,[4,5,6]].flat())
const a=()=>console.log(1)
console.log(Object.fromEntries( [["a",1],["b",2]]))
console.log(a.toString())
`))))
e.Await()
}
func TestEs2020(t *testing.T) {
e := NewEngine()
println(IsNullish(fn.Panic1(e.RunString(
//language=javascript
`
console.log(globalThis)
// import('./promise.js').then(r=>console.log("supported"))
// console.log(BigInt("0x1fffffffffffff"))
console.log(null ?? 10)
console.log(undefined ?? 10)
console.log(false ?? 10)
const a={b:{c:'c'}}
console.log(a.b?.b?.c)
`))))
e.Await()
}
func TestEs2021(t *testing.T) {
e := NewEngine()
println(IsNullish(fn.Panic1(e.RunString(
//language=javascript
`
const str = 'sdfoooodbb'
const newStr = str.replaceAll('o', 'z')
console.log(newStr)
// console.log(1_0000_0000)
`))))
e.Await()
}
func TestEs2022(t *testing.T) {
e := NewEngine()
println(IsNullish(fn.Panic1(e.RunString(
//language=javascript
`
class C1 {
myName = '11一碗'
#dsdf='123'
get x(){
return this.#dsdf
}
}
class C2 {
constructor() {
this.myName = "fdafzhah汉族"
}
}
console.log(new C1(),new C2())
console.log(new C1().x)
//TOP level await not supported yet
const b=()=>new Promise((r,j)=>r(1))
const c= async ()=> await b()
console.log(typeof b)
console.log(b.name)
c().then(v=>console.log('future',v))
// console.log(await c()) TOP Level not supported yet
console.log(Object.hasOwn(new C1(),'#dsdf'))
console.log(Object.hasOwn(new C1(),'myName'))
console.log([1,2,3].at(-1))
`))))
e.Await()
}
func TestSimpleRequire(t *testing.T) {
fn.Panic(os.WriteFile("simple.js", []byte(`
export function some() {
return 1
}`), os.ModePerm))
e := Get()
defer e.Free()
println(IsNullish(fn.Panic1(e.RunTs(`
import {some} from './simple.js'
console.log(typeof some)
console.log(some())
`))))
println(IsNullish(fn.Panic1(e.RunJs(`
import {some} from './simple.js'
console.log(typeof some)
console.log(some())
`))))
_ = os.Remove("simple.js")
}
func TestSimpleRequire2(t *testing.T) {
fn.Panic(os.WriteFile("simple.js", []byte(`
export function some() {
return 1
}`), os.ModePerm))
e := Get()
defer e.Free()
println(IsNullish(fn.Panic1(e.RunTs(`
import {some} from './simple.js'
export const a=1
console.log(typeof some)
console.log(some())
console.log(a)
`))))
println(IsNullish(fn.Panic1(e.RunTs(`
import {some} from './simple.js'
export const b=1
console.log(typeof some)
console.log(some())
console.log(b)
`))))
_ = os.Remove("simple.js")
}