dop251 / goja

ECMAScript/JavaScript engine in pure Go
MIT License
5.63k stars 378 forks source link

abnormal string input to js function #577

Closed zhu659zhu closed 5 months ago

zhu659zhu commented 5 months ago

When the input contains special characters, the value of the input JS function does not meet the expectations.

example:

vm := goja.New()
_, err := vm.RunString("function sum(a) {return a.length;}")
if err != nil {
    panic(err)
}
sum, ok := goja.AssertFunction(vm.Get("sum"))
if !ok {
    panic("Not a function")
}
res, err := sum(goja.Undefined(), vm.ToValue(string([]byte{206, 176})))
if err != nil {
    panic(err)
}
fmt.Println(res)

the output is 1

i think 2 is as expected.

dop251 commented 5 months ago

ECMAScript operates in UCS2, therefore the supplied string is converted from UTF-8 to UCS which in your case represents a single code unit. An equivalent JS code:

const b = new Uint8Array([206, 176]);
console.log(new TextDecoder('utf-8').decode(b).length);

which prints 1.