mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-06-19 01:41:30 +00:00
Initial new rt integration
This commit is contained in:
@ -1,23 +1,31 @@
|
||||
"use strict";
|
||||
|
||||
const hasBigInt64 = typeof BigUint64Array !== "undefined";
|
||||
const thisPtr = Symbol();
|
||||
/** Size of the runtime header, in bytes. */
|
||||
const HEADER_SIZE = 16;
|
||||
/** Runtime header offset of `classId`. */
|
||||
const CLASSID_OFFSET = -HEADER_SIZE;
|
||||
/** Runtime header offset of `payloadLength`. */
|
||||
const PAYLOADLENGTH_OFFSET = -HEADER_SIZE + 4;
|
||||
/** Whether BigInt arrays are supported. */
|
||||
const SUPPORTS_BIGINT = typeof BigUint64Array !== "undefined";
|
||||
/** Unique symbol for memoized 'this'. */
|
||||
const THIS = Symbol();
|
||||
|
||||
/** Gets a string from an U32 and an U16 view on a memory. */
|
||||
function getStringImpl(U32, U16, ptr) {
|
||||
var dataLength = U32[ptr >>> 2];
|
||||
var dataOffset = (ptr + 4) >>> 1;
|
||||
var dataRemain = dataLength;
|
||||
var size32 = U32[(ptr + PAYLOADLENGTH_OFFSET) >>> 2];
|
||||
var offset16 = ptr >>> 1;
|
||||
var remain32 = size32;
|
||||
var parts = [];
|
||||
const chunkSize = 1024;
|
||||
while (dataRemain > chunkSize) {
|
||||
let last = U16[dataOffset + chunkSize - 1];
|
||||
while (remain32 > chunkSize) {
|
||||
let last = U16[offset16 + chunkSize - 1];
|
||||
let size = last >= 0xD800 && last < 0xDC00 ? chunkSize - 1 : chunkSize;
|
||||
let part = U16.subarray(dataOffset, dataOffset += size);
|
||||
let part = U16.subarray(offset16, offset16 += size);
|
||||
parts.push(String.fromCharCode.apply(String, part));
|
||||
dataRemain -= size;
|
||||
remain32 -= size;
|
||||
}
|
||||
return parts.join("") + String.fromCharCode.apply(String, U16.subarray(dataOffset, dataOffset + dataRemain));
|
||||
return parts.join("") + String.fromCharCode.apply(String, U16.subarray(offset16, offset16 + remain32));
|
||||
}
|
||||
|
||||
/** Prepares the base module prior to instantiation. */
|
||||
@ -54,8 +62,7 @@ function postInstantiate(baseModule, instance) {
|
||||
var memory_fill = rawExports["memory.fill"];
|
||||
var memory_free = rawExports["memory.free"];
|
||||
var table = rawExports.table;
|
||||
var capabilities = rawExports[".capabilities"] || 0;
|
||||
var setargc = rawExports[".setargc"] || function() {};
|
||||
var setargc = rawExports["$.setArgc"] || function() {};
|
||||
|
||||
// Provide views for all sorts of basic values
|
||||
var buffer, I8, U8, I16, U16, I32, U32, F32, F64, I64, U64;
|
||||
@ -71,7 +78,7 @@ function postInstantiate(baseModule, instance) {
|
||||
U16 = new Uint16Array(buffer);
|
||||
I32 = new Int32Array(buffer);
|
||||
U32 = new Uint32Array(buffer);
|
||||
if (hasBigInt64) {
|
||||
if (SUPPORTS_BIGINT) {
|
||||
I64 = new BigInt64Array(buffer);
|
||||
U64 = new BigUint64Array(buffer);
|
||||
}
|
||||
@ -271,7 +278,7 @@ function demangle(exports, baseModule) {
|
||||
};
|
||||
ctor.prototype = {};
|
||||
ctor.wrap = function(thisValue) {
|
||||
return Object.create(ctor.prototype, { [thisPtr]: { value: thisValue, writable: false } });
|
||||
return Object.create(ctor.prototype, { [THIS]: { value: thisValue, writable: false } });
|
||||
};
|
||||
if (classElem) Object.getOwnPropertyNames(classElem).forEach(name =>
|
||||
Object.defineProperty(ctor, name, Object.getOwnPropertyDescriptor(classElem, name))
|
||||
@ -285,8 +292,8 @@ function demangle(exports, baseModule) {
|
||||
let getter = exports[internalName.replace("set:", "get:")];
|
||||
let setter = exports[internalName.replace("get:", "set:")];
|
||||
Object.defineProperty(curr, name, {
|
||||
get: function() { return getter(this[thisPtr]); },
|
||||
set: function(value) { setter(this[thisPtr], value); },
|
||||
get: function() { return getter(this[THIS]); },
|
||||
set: function(value) { setter(this[THIS], value); },
|
||||
enumerable: true
|
||||
});
|
||||
}
|
||||
@ -297,7 +304,7 @@ function demangle(exports, baseModule) {
|
||||
Object.defineProperty(curr, name, {
|
||||
value: function (...args) {
|
||||
setargc(args.length);
|
||||
return elem(this[thisPtr], ...args);
|
||||
return elem(this[THIS], ...args);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
Reference in New Issue
Block a user