refactor(common): 重构 Conn 实体并优化地图进入逻辑
- 优化 Conn 实体的 SendPack 方法,提高代码复用性 - 添加 goja 模块到 go.work 文件 - 重构地图进入逻辑,增加玩家广播和刷怪功能 - 调整 OutInfo 结构中的 Vip 和 Viped 字段类型 - 简化 MonsterRefresh 结构体定义
This commit is contained in:
133
common/utils/goja/array_test.go
Normal file
133
common/utils/goja/array_test.go
Normal file
@@ -0,0 +1,133 @@
|
||||
package goja
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestArray1(t *testing.T) {
|
||||
r := &Runtime{}
|
||||
a := r.newArray(nil)
|
||||
a.setOwnIdx(valueInt(0), asciiString("test"), true)
|
||||
if l := a.getStr("length", nil).ToInteger(); l != 1 {
|
||||
t.Fatalf("Unexpected length: %d", l)
|
||||
}
|
||||
}
|
||||
|
||||
func TestArrayExportProps(t *testing.T) {
|
||||
vm := New()
|
||||
arr := vm.NewArray()
|
||||
err := arr.DefineDataProperty("0", vm.ToValue(true), FLAG_TRUE, FLAG_FALSE, FLAG_TRUE)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
actual := arr.Export()
|
||||
expected := []interface{}{true}
|
||||
if !reflect.DeepEqual(actual, expected) {
|
||||
t.Fatalf("Expected: %#v, actual: %#v", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestArrayCanonicalIndex(t *testing.T) {
|
||||
const SCRIPT = `
|
||||
var a = [];
|
||||
a["00"] = 1;
|
||||
a["01"] = 2;
|
||||
if (a[0] !== undefined) {
|
||||
throw new Error("a[0]");
|
||||
}
|
||||
`
|
||||
|
||||
testScript(SCRIPT, _undefined, t)
|
||||
}
|
||||
|
||||
func BenchmarkArrayGetStr(b *testing.B) {
|
||||
b.StopTimer()
|
||||
r := New()
|
||||
v := &Object{runtime: r}
|
||||
|
||||
a := &arrayObject{
|
||||
baseObject: baseObject{
|
||||
val: v,
|
||||
extensible: true,
|
||||
},
|
||||
}
|
||||
v.self = a
|
||||
|
||||
a.init()
|
||||
|
||||
v.setOwn(valueInt(0), asciiString("test"), false)
|
||||
b.StartTimer()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
a.getStr("0", nil)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func BenchmarkArrayGet(b *testing.B) {
|
||||
b.StopTimer()
|
||||
r := New()
|
||||
v := &Object{runtime: r}
|
||||
|
||||
a := &arrayObject{
|
||||
baseObject: baseObject{
|
||||
val: v,
|
||||
extensible: true,
|
||||
},
|
||||
}
|
||||
v.self = a
|
||||
|
||||
a.init()
|
||||
|
||||
var idx Value = valueInt(0)
|
||||
|
||||
v.setOwn(idx, asciiString("test"), false)
|
||||
|
||||
b.StartTimer()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
v.get(idx, nil)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func BenchmarkArrayPut(b *testing.B) {
|
||||
b.StopTimer()
|
||||
r := New()
|
||||
|
||||
v := &Object{runtime: r}
|
||||
|
||||
a := &arrayObject{
|
||||
baseObject: baseObject{
|
||||
val: v,
|
||||
extensible: true,
|
||||
},
|
||||
}
|
||||
|
||||
v.self = a
|
||||
|
||||
a.init()
|
||||
|
||||
var idx Value = valueInt(0)
|
||||
var val Value = asciiString("test")
|
||||
|
||||
b.StartTimer()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
v.setOwn(idx, val, false)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func BenchmarkArraySetEmpty(b *testing.B) {
|
||||
r := New()
|
||||
_ = r.Get("Array").(*Object).Get("prototype").String() // materialise Array.prototype
|
||||
a := r.NewArray(0, 0)
|
||||
values := a.self.(*arrayObject).values
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
values[0] = nil
|
||||
a.self.setOwnIdx(0, valueTrue, true)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user