@montferret/ferret-wasm
Version:
Ferret compiler and runtime ported to WASM
208 lines (207 loc) • 8.54 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var is_node_1 = __importDefault(require("./is-node"));
var Platform = /** @class */ (function () {
function Platform(encoding) {
var _this = this;
if (encoding === void 0) { encoding = 'utf-8'; }
var env = (is_node_1.default ? global : window);
this.Array = env.Array;
this.ArrayBuffer = env.ArrayBuffer;
this.Boolean = env.Boolean;
this.Buffer = env.Buffer;
this.DataView = env.DataView;
this.Date = env.Date;
this.Error = env.Error;
this.EvalError = env.EvalError;
this.Float32Array = env.Float32Array;
this.Float64Array = env.Float64Array;
this.Function = env.Function;
this.GLOBAL = this;
this.Infinity = env.Infinity;
this.Int8Array = env.Int8Array;
this.Int16Array = env.Int16Array;
this.Int32Array = env.Int32Array;
this.Intl = env.Intl;
this.JSON = env.JSON;
this.Map = env.Map;
this.Math = env.Math;
this.NaN = env.NaN;
this.Number = env.Number;
this.Object = env.Object;
this.Promise = env.Promise;
this.RangeError = env.RangeError;
this.ReferenceError = env.ReferenceError;
this.RegExp = env.RegExp;
this.Set = env.Set;
this.String = env.String;
this.Symbol = env.Symbol;
this.SyntaxError = env.SyntaxError;
this.TypeError = env.TypeError;
this.URIError = env.URIError;
this.Uint8Array = env.Uint8Array;
this.Uint8ClampedArray = env.Uint8ClampedArray;
this.Uint16Array = env.Uint16Array;
this.Uint32Array = env.Uint32Array;
this.WeakMap = env.WeakMap;
this.WeakSet = env.WeakSet;
this.clearImmediate = env.clearImmediate;
this.clearInterval = env.clearInterval;
this.clearTimeout = env.clearTimeout;
this.console = env.console;
this.decodeURI = env.decodeURI;
this.decodeURIComponent = env.decodeURIComponent;
this.encodeURI = env.encodeURI;
this.encodeURIComponent = env.encodeURIComponent;
this.escape = env.escape;
this.eval = env.eval;
this.gc = is_node_1.default ? env.gc : function () { };
this.global = this;
this.isFinite = env.isFinite;
this.isNaN = env.isNaN;
this.parseFloat = env.parseFloat;
this.parseInt = env.parseInt;
this.process = env.process;
this.queueMicrotask = env.queueMicrotask;
this.root = this;
this.setImmediate = env.setImmediate;
this.setInterval = env.setInterval;
this.setTimeout = env.setTimeout;
this.undefined = env.undefined;
this.unescape = env.unescape;
this.v8debug = env.v8debug;
if (is_node_1.default) {
var fs = require('fs');
var nodeCrypto_1 = require('crypto');
this.fs = fs;
this.crypto = {
getRandomValues: function (b) {
nodeCrypto_1.randomFillSync(b);
},
};
this.performance = {
now: function () {
var _a = process.hrtime(), sec = _a[0], nsec = _a[1];
return sec * 1000 + nsec / 1000000;
},
};
var util = require('util');
this.encoder = new util.TextEncoder(encoding);
this.decoder = new util.TextDecoder(encoding);
var webStreams_1 = require('web-streams-polyfill/dist/ponyfill');
this['ReadableStream'] = webStreams_1.ReadableStream;
this['WritableStream'] = webStreams_1.WritableStream;
this['ByteLengthQueuingStrategy'] =
webStreams_1.ByteLengthQueuingStrategy;
this['CountQueuingStrategy'] = webStreams_1.CountQueuingStrategy;
this['TransformStream'] = webStreams_1.TransformStream;
var fetch_1 = require('cross-fetch/dist/node-ponyfill');
this['fetch'] = function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
return fetch_1.apply(void 0, args).then(function (res) {
var body = res.body;
body.getReader = function () {
var stream = new webStreams_1.ReadableStream({
start: function (controller) {
res.body.on('data', function (chunk) {
controller.enqueue(chunk);
});
res.body.on('end', function () {
controller.close();
});
// res.body.on('close', () => {
// controller.close();
// });
res.body.on('error', function (err) {
controller.error(err);
});
},
});
return stream.getReader();
};
return res;
});
};
this['Headers'] = fetch_1.Headers;
this['Request'] = fetch_1.Request;
this['Response'] = fetch_1.Response;
}
else {
var outputBuf_1 = '';
var platform_1 = this;
this.fs = {
constants: {
O_WRONLY: -1,
O_RDWR: -1,
O_CREAT: -1,
O_TRUNC: -1,
O_APPEND: -1,
O_EXCL: -1,
},
writeSync: function (fd, buf) {
outputBuf_1 += platform_1.decoder.decode(buf);
var nl = outputBuf_1.lastIndexOf('\n');
if (nl != -1) {
console.log(outputBuf_1.substr(0, nl));
outputBuf_1 = outputBuf_1.substr(nl + 1);
}
return buf.length;
},
write: function (fd, buf, offset, length, position, callback) {
if (offset !== 0 ||
length !== buf.length ||
position !== null) {
throw new Error('not implemented');
}
var n = this.writeSync(fd, buf);
callback(null, n);
},
open: function (path, flags, mode, callback) {
var err = new Error('not implemented');
err.code = 'ENOSYS';
callback(err);
},
read: function (fd, buffer, offset, length, position, callback) {
var err = new Error('not implemented');
err.code = 'ENOSYS';
callback(err);
},
fsync: function (fd, callback) {
callback(null);
},
};
this.crypto = window.crypto;
this.performance = window.performance;
this.encoder = new window.TextEncoder(encoding);
this.decoder = new window.TextDecoder(encoding);
var obj = window;
do
Object.getOwnPropertyNames(obj).forEach(function (key) {
// skip already defined keys
if (_this[key] != null) {
return;
}
var value = env[key];
// skip leaking env object
if (value === env) {
return;
}
if (key[0].toLowerCase() === key[0]) {
if (typeof value === 'function') {
value = value.bind(env);
}
}
_this[key] = value;
});
while (obj == Object.getPrototypeOf(obj));
}
}
return Platform;
}());
exports.Platform = Platform;