JS: refactor array implementation in native JS

This commit is contained in:
Alexey Andreev 2023-10-18 20:47:14 +02:00
parent bb0d4a4fc6
commit 60d0b07f83
2 changed files with 68 additions and 63 deletions

View File

@ -14,35 +14,11 @@
* limitations under the License. * limitations under the License.
*/ */
function $rt_array(cls, data) { var $rt_booleanArrayCls = $rt_arraycls($rt_booleancls());
$rt_initMonitorField(this); var $rt_charArrayCls = $rt_arraycls($rt_charcls());
this.$id$ = 0; var $rt_byteArrayCls = $rt_arraycls($rt_bytecls());
this.type = cls; var $rt_shortArrayCls = $rt_arraycls($rt_shortcls());
this.data = data; var $rt_intArrayCls = $rt_arraycls($rt_intcls());
this.constructor = $rt_arraycls(cls); var $rt_longArrayCls = $rt_arraycls($rt_longcls());
} var $rt_floatArrayCls = $rt_arraycls($rt_floatcls());
$rt_array.prototype = Object.create($rt_objcls().prototype); var $rt_doubleArrayCls = $rt_arraycls($rt_doublecls());
$rt_array.prototype.toString = function() {
var str = "[";
for (var i = 0; i < this.data.length; ++i) {
if (i > 0) {
str += ", ";
}
str += this.data[i].toString();
}
str += "]";
return str;
};
$rt_setCloneMethod($rt_array.prototype, function () {
var dataCopy;
if ('slice' in this.data) {
dataCopy = this.data.slice();
} else {
dataCopy = new this.data.constructor(this.data.length);
for (var i = 0; i < dataCopy.length; ++i) {
dataCopy[i] = this.data[i];
}
}
return new $rt_array(this.type, dataCopy);
});

View File

@ -59,105 +59,133 @@ function $rt_castToClass(obj, cls) {
function $rt_createArray(cls, sz) { function $rt_createArray(cls, sz) {
var data = new Array(sz); var data = new Array(sz);
data.fill(null); data.fill(null);
return new $rt_array(cls, data); return new ($rt_arraycls(cls))(data);
} }
function $rt_createArrayFromData(cls, init) { function $rt_createArrayFromData(cls, init) {
return $rt_wrapArray(cls, init); return $rt_wrapArray(cls, init);
} }
function $rt_wrapArray(cls, data) { function $rt_wrapArray(cls, data) {
return new $rt_array(cls, data); return new ($rt_arraycls(cls))(data);
} }
function $rt_createUnfilledArray(cls, sz) { function $rt_createUnfilledArray(cls, sz) {
return new $rt_array(cls, new Array(sz)); return new ($rt_arraycls(cls))(new Array(sz));
}
function $rt_createNumericArray(cls, nativeArray) {
return new $rt_array(cls, nativeArray);
} }
var $rt_createLongArray; var $rt_createLongArray;
var $rt_createLongArrayFromData; var $rt_createLongArrayFromData;
if (typeof BigInt64Array !== 'function') { if (typeof BigInt64Array !== 'function') {
$rt_createLongArray = function(sz) { $rt_createLongArray = function(sz) {
var data = new Array(sz); var data = new Array(sz);
var arr = new $rt_array($rt_longcls(), data); var arr = new $rt_longArrayCls(data);
data.fill(Long_ZERO); data.fill(Long_ZERO);
return arr; return arr;
} }
$rt_createLongArrayFromData = function(init) { $rt_createLongArrayFromData = function(init) {
return new $rt_array($rt_longcls(), init); return new $rt_longArrayCls(init);
} }
} else { } else {
$rt_createLongArray = function (sz) { $rt_createLongArray = function (sz) {
return $rt_createNumericArray($rt_longcls(), new BigInt64Array(sz)); return new $rt_longArrayCls(new BigInt64Array(sz));
} }
$rt_createLongArrayFromData = function(data) { $rt_createLongArrayFromData = function(data) {
var buffer = new BigInt64Array(data.length); var buffer = new BigInt64Array(data.length);
buffer.set(data); buffer.set(data);
return $rt_createNumericArray($rt_longcls(), buffer); return new $rt_longArrayCls(buffer);
} }
} }
function $rt_createCharArray(sz) { function $rt_createCharArray(sz) {
return $rt_createNumericArray($rt_charcls(), new Uint16Array(sz)); return new $rt_charArrayCls(new Uint16Array(sz));
} }
function $rt_createCharArrayFromData(data) { function $rt_createCharArrayFromData(data) {
var buffer = new Uint16Array(data.length); var buffer = new Uint16Array(data.length);
buffer.set(data); buffer.set(data);
return $rt_createNumericArray($rt_charcls(), buffer); return new $rt_charArrayCls(buffer);
} }
function $rt_createByteArray(sz) { function $rt_createByteArray(sz) {
return $rt_createNumericArray($rt_bytecls(), new Int8Array(sz)); return new $rt_byteArrayCls(new Int8Array(sz));
} }
function $rt_createByteArrayFromData(data) { function $rt_createByteArrayFromData(data) {
var buffer = new Int8Array(data.length); var buffer = new Int8Array(data.length);
buffer.set(data); buffer.set(data);
return $rt_createNumericArray($rt_bytecls(), buffer); return new $rt_byteArrayCls(buffer);
} }
function $rt_createShortArray(sz) { function $rt_createShortArray(sz) {
return $rt_createNumericArray($rt_shortcls(), new Int16Array(sz)); return new $rt_shortArrayCls(new Int16Array(sz));
} }
function $rt_createShortArrayFromData(data) { function $rt_createShortArrayFromData(data) {
var buffer = new Int16Array(data.length); var buffer = new Int16Array(data.length);
buffer.set(data); buffer.set(data);
return $rt_createNumericArray($rt_shortcls(), buffer); return new $rt_shortArrayCls(buffer);
} }
function $rt_createIntArray(sz) { function $rt_createIntArray(sz) {
return $rt_createNumericArray($rt_intcls(), new Int32Array(sz)); return new $rt_intArrayCls(new Int32Array(sz));
} }
function $rt_createIntArrayFromData(data) { function $rt_createIntArrayFromData(data) {
var buffer = new Int32Array(data.length); var buffer = new Int32Array(data.length);
buffer.set(data); buffer.set(data);
return $rt_createNumericArray($rt_intcls(), buffer); return new $rt_intArrayCls(buffer);
} }
function $rt_createBooleanArray(sz) { function $rt_createBooleanArray(sz) {
return $rt_createNumericArray($rt_booleancls(), new Int8Array(sz)); return new $rt_booleanArrayCls(new Int8Array(sz));
} }
function $rt_createBooleanArrayFromData(data) { function $rt_createBooleanArrayFromData(data) {
var buffer = new Int8Array(data.length); var buffer = new Int8Array(data.length);
buffer.set(data); buffer.set(data);
return $rt_createNumericArray($rt_booleancls(), buffer); return new $rt_booleanArrayCls(buffer);
} }
function $rt_createFloatArray(sz) { function $rt_createFloatArray(sz) {
return $rt_createNumericArray($rt_floatcls(), new Float32Array(sz)); return new $rt_floatArrayCls(new Float32Array(sz));
} }
function $rt_createFloatArrayFromData(data) { function $rt_createFloatArrayFromData(data) {
var buffer = new Float32Array(data.length); var buffer = new Float32Array(data.length);
buffer.set(data); buffer.set(data);
return $rt_createNumericArray($rt_floatcls(), buffer); return new $rt_floatArrayCls(buffer);
} }
function $rt_createDoubleArray(sz) { function $rt_createDoubleArray(sz) {
return $rt_createNumericArray($rt_doublecls(), new Float64Array(sz)); return new $rt_doubleArrayCls(new Float64Array(sz));
} }
function $rt_createDoubleArrayFromData(data) { function $rt_createDoubleArrayFromData(data) {
var buffer = new Float64Array(data.length); var buffer = new Float64Array(data.length);
buffer.set(data); buffer.set(data);
return $rt_createNumericArray($rt_doublecls(), buffer); return new $rt_doubleArrayCls(buffer);
} }
function $rt_arraycls(cls) { function $rt_arraycls(cls) {
var result = cls.$array; var result = cls.$array;
if (result === null) { if (result === null) {
var arraycls = {}; function JavaArray(data) {
$rt_objcls().call(this);
this.data = data;
}
JavaArray.prototype = Object.create($rt_objcls().prototype);
JavaArray.prototype.type = cls;
JavaArray.prototype.constructor = JavaArray;
JavaArray.prototype.toString = function() {
var str = "[";
for (var i = 0; i < this.data.length; ++i) {
if (i > 0) {
str += ", ";
}
str += this.data[i].toString();
}
str += "]";
return str;
};
$rt_setCloneMethod(JavaArray.prototype, function () {
var dataCopy;
if ('slice' in this.data) {
dataCopy = this.data.slice();
} else {
dataCopy = new this.data.constructor(this.data.length);
for (var i = 0; i < dataCopy.length; ++i) {
dataCopy[i] = this.data[i];
}
}
return new ($rt_arraycls(this.type))(dataCopy);
});
var name = "[" + cls.$meta.binaryName; var name = "[" + cls.$meta.binaryName;
arraycls.$meta = { JavaArray.$meta = {
item: cls, item: cls,
supertypes: [$rt_objcls()], supertypes: [$rt_objcls()],
primitive: false, primitive: false,
@ -169,10 +197,11 @@ function $rt_arraycls(cls) {
declaringClass: null, declaringClass: null,
enclosingClass: null enclosingClass: null
}; };
arraycls.classObject = null; JavaArray.classObject = null;
arraycls.$array = null; JavaArray.$array = null;
result = arraycls;
cls.$array = arraycls; result = JavaArray;
cls.$array = JavaArray;
} }
return result; return result;
} }
@ -945,7 +974,7 @@ function $rt_fastStringToCharArray(string) {
for (var i = 0; i < array.length; ++i) { for (var i = 0; i < array.length; ++i) {
array[i] = string.charCodeAt(i); array[i] = string.charCodeAt(i);
} }
return $rt_createNumericArray($rt_charcls(), array); return new $rt_charArrayCls(array);
} }
function $rt_substring(string, start, end) { function $rt_substring(string, start, end) {
if (start === 0 && end === string.length) { if (start === 0 && end === string.length) {