wll8 / sys-shim

使用前端语言快速实现桌面程序。
https://wll8.github.io/sys-shim-doc/
225 stars 17 forks source link

不能正常处理稀疏数组 #8

Open wll8 opened 2 months ago

wll8 commented 2 months ago

同样是中间有 null 的两个数组,为什么第一个数组出现了数据丢失?

起因

await native.unpack([1, null, 2]) // 错误
// 实例输出 [undefined, 1]
// 预期输出 [undefined, 1, null, 2]

await native.table.unpack([1, 2, null, 3]) // 正确
// 实例输出 [undefined, 1, 2, null, 3]
// 预期输出 [undefined, 1, 2, null, 3]

排查

在原生代码中 使用 thread 可以重现此问题:

import console; 
import thread.command
console.open()

var listener = thread.command.instance();
listener.publish = function(...){
  var data = {...}
  console.writeText(web.json.stringify(data), '---\r\n')
}
thread.command.publish({null, 1, 3}) // 输出 [{}] -- 数据丢失
thread.command.publish({1, null, 3}) // 输出 [[1]] -- 数据丢失
thread.command.publish({1, 2, null, 3}) // 输出 [[1,2,null,3]] -- 数据没有丢失

import win
win.loopMessage()

在原生代码中 不使用 thread 没有重现此问题:

import console

fn = function(...){
  var data = {...}
  return table.unpack(data); 
}

console.dumpJson(fn({1, null, 3})) // 输出 [1, null, 3]

import web.json
console.writeText(web.json.stringify({1, null, 3}), '---\r\n') // 输出 [1, null, 3]

import win
win.loopMessage()

环境

wll8 commented 2 months ago
aardio 作者回复: ![image](https://github.com/wll8/sys-shim/assets/28223260/6b6aa06f-4e63-427c-b054-7c2a6647fa62)

aardio 文档:

image

数组含 null 实例输出:

代码:

import console
var array = { "值:1", null, "值:2", "值:4" } 

console.dump(array)
import win
win.loopMessage()

输出:

{
[1]="值:1";
[3]="值:2";
[4]="值:4"
}table: 029F4860
wll8 commented 2 months ago

Deleted.