This commit is contained in:
2025-05-27 21:24:22 +08:00
parent e1528c41bb
commit d71fc5c822
126 changed files with 11382 additions and 1202 deletions

View File

@@ -27,7 +27,7 @@ class JSONParser {
if (vargc > 0) converter = vargc[0];
// actions for string tokens
local string = {
go = function() {
state = "ok";
@@ -55,7 +55,7 @@ class JSONParser {
}.bindenv(this)
};
// the actions for number tokens
local number = {
go = function() {
state = "ok";
@@ -75,8 +75,8 @@ class JSONParser {
};
// action table
// describes where the state machine will go from each given state
local action = {};
@@ -188,9 +188,9 @@ class JSONParser {
action[":"] <- {
colon = function() {
// Check if the key already exists
// NOTE previous code used 'if (key in container)...'
// but this finds table ('container') member methods too
local err = false;
foreach(akey, avalue in container) {
if (akey == key) err = true;
@@ -271,11 +271,11 @@ class JSONParser {
//
state = "go";
stack = [];
// current tokenizeing position
local start = 0;
try {
@@ -285,14 +285,14 @@ class JSONParser {
while (token = tokenizer.nextToken(str, start)) {
if ("ptfn" == token.type) {
// punctuation/true/false/null
action[token.value][state]();
} else if ("number" == token.type) {
// number
value = token.value;
number[state]();
} else if ("string" == token.type) {
// string
value = tokenizer.unescape(token.value);
string[state]();
}
@@ -322,11 +322,11 @@ class JSONParser {
function _convert(value, type, converter) {
if ("function" == typeof converter) {
// # of params for converter function
local parametercCount = 2;
// .getinfos() is missing on ei platform
if ("getinfos" in converter) {
parametercCount = converter.getinfos().parameters.len() -
1;
@@ -361,19 +361,19 @@ class _JSONTokenizer {
_unescapeRegex = null;
constructor() {
// punctuation/true/false/null
this._ptfnRegex = regexp("^(?:\\,|\\:|\\[|\\]|\\{|\\}|true|false|null)");
// numbers
this._numberRegex = regexp("^(?:\\-?\\d+(?:\\.\\d*)?(?:[eE][+\\-]?\\d+)?)");
// strings
this._stringRegex = regexp("^(?:\\\"((?:[^\\r\\n\\t\\\\\\\"]|\\\\(?:[\"\\\\\\/trnfb]|u[0-9a-fA-F]{4}))*)\\\")");
// ltrim pattern
this._ltrimRegex = regexp("^[\\s\\t\\n\\r]*");
// string unescaper tokenizer pattern
this._unescapeRegex = regexp("\\\\(?:(?:u\\d{4})|[\\\"\\\\/bfnrt])");
}
@@ -389,20 +389,20 @@ class _JSONTokenizer {
length,
whitespaces;
// count # of left-side whitespace chars
whitespaces = this._leadingWhitespaces(str, start);
start += whitespaces;
if (m = this._ptfnRegex.capture(str, start)) {
// punctuation/true/false/null
value = str.slice(m[0].begin, m[0].end);
type = "ptfn";
} else if (m = this._numberRegex.capture(str, start)) {
// number
value = str.slice(m[0].begin, m[0].end);
type = "number";
} else if (m = this._stringRegex.capture(str, start)) {
// string
value = str.slice(m[1].begin, m[1].end);
type = "string";
} else {
@@ -429,7 +429,7 @@ class _JSONTokenizer {
}
}
// unesacape() replacements table
_unescapeReplacements = {
b = "\b",
f = "\f",
@@ -449,7 +449,7 @@ class _JSONTokenizer {
if (m) {
local token = str.slice(m[0].begin, m[0].end);
// append chars before match
local pre = str.slice(start, m[0].begin);
res += pre;
@@ -482,8 +482,8 @@ class JSONEncoder {
static VERSION = "2.0.0";
// max structure depth
// anything above probably has a cyclic ref
static _maxDepth = 32;
@@ -495,7 +495,7 @@ class JSONEncoder {
function _encode(val, ...) {
local depth = 0;
if (vargc > 0) depth = vargv[0];
// detect cyclic reference
if (depth > this._maxDepth) {
throw "Possible cyclic reference";
}
@@ -511,7 +511,7 @@ class JSONEncoder {
case "class":
s = "";
// serialize properties, but not functions
foreach(k, v in val) {
if (typeof v != "function") {
s += ",\"" + k + "\":" + this._encode(v, depth + 1);
@@ -547,12 +547,12 @@ class JSONEncoder {
if ("_serializeRaw" in val && typeof val._serializeRaw == "function") {
// include value produced by _serializeRaw()
r += val._serializeRaw().tostring();
} else if ("_serialize" in val && typeof val._serialize == "function") {
// serialize instances by calling _serialize method
r += this._encode(val._serialize(), depth + 1);
} else {
@@ -561,15 +561,15 @@ class JSONEncoder {
try {
// iterate through instances which implement _nexti meta-method
foreach(k, v in val) {
s += ",\"" + k + "\":" + this._encode(v, depth + 1);
}
} catch (e) {
// iterate through instances w/o _nexti
// serialize properties, but not functions
foreach(k, v in val.getclass()) {
if (typeof v != "function") {
s += ",\"" + k + "\":" + this._encode(val[k], depth + 1);
@@ -585,13 +585,13 @@ class JSONEncoder {
break;
case "blob":
// This is a workaround for a known bug:
// on device side Blob.tostring() returns null
// (instaead of an empty string)
r += "\"" + (val.len() ? this._escape(val.tostring()) : "") + "\"";
break;
// strings and all other
default:
r += "\"" + this._escape(val.tostring()) + "\"";
break;
@@ -608,7 +608,7 @@ class JSONEncoder {
local ch1 = (str[i] & 0xFF);
if ((ch1 & 0x80) == 0x00) {
// 7-bit Ascii
ch1 = format("%c", ch1);
@@ -637,16 +637,16 @@ class JSONEncoder {
} else {
if ((ch1 & 0xE0) == 0xC0) {
// 110xxxxx = 2-byte unicode
local ch2 = (str[++i] & 0xFF);
res += format("%c%c", ch1, ch2);
} else if ((ch1 & 0xF0) == 0xE0) {
// 1110xxxx = 3-byte unicode
local ch2 = (str[++i] & 0xFF);
local ch3 = (str[++i] & 0xFF);
res += format("%c%c%c", ch1, ch2, ch3);
} else if ((ch1 & 0xF8) == 0xF0) {
// 11110xxx = 4 byte unicode
local ch2 = (str[++i] & 0xFF);
local ch3 = (str[++i] & 0xFF);
local ch4 = (str[++i] & 0xFF);