Files
discord-decomp/_hermes/prelude.js
T
2026-11-01 12:12:55 +02:00

188 lines
6.2 KiB
JavaScript

// Runtime helpers referenced by decompiled Hermes bytecode.
var HermesBuiltin = (function () {
var templates = {};
return {
silentSetPrototypeOf: function (object, proto) {
try {
Object.setPrototypeOf(object, proto);
} catch (e) {}
},
getTemplateObject: function (id, dup) {
if (templates[id]) return templates[id];
var strings = Array.prototype.slice.call(arguments, 2);
var raw = dup ? strings : strings.slice(0, strings.length / 2);
var cooked = dup ? strings.slice() : strings.slice(strings.length / 2);
Object.defineProperty(cooked, "raw", { value: Object.freeze(raw.slice()) });
return (templates[id] = Object.freeze(cooked));
},
ensureObject: function (value, message) {
if ((typeof value !== "object" || value === null) && typeof value !== "function") {
throw new TypeError(message);
}
},
getMethod: function (object, key) {
var method = object[key];
if (method === undefined || method === null) return undefined;
if (typeof method !== "function") throw new TypeError("not a function");
return method;
},
throwTypeError: function (message) {
throw new TypeError(message);
},
generatorSetDelegated: function () {},
copyDataProperties: function (target, source, excluded) {
if (source === null || source === undefined) return target;
var keys = Reflect.ownKeys(Object(source));
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
if (excluded && Object.prototype.hasOwnProperty.call(excluded, key)) continue;
if (Object.prototype.propertyIsEnumerable.call(source, key)) target[key] = source[key];
}
return target;
},
arraySpread: function (target, source, index) {
var iterator = source[Symbol.iterator]();
for (var step = iterator.next(); !step.done; step = iterator.next()) {
target[index++] = step.value;
}
return index;
},
apply: function (fn, args, thisArg) {
return arguments.length < 3 ? Reflect.construct(fn, args) : fn.apply(thisArg, args);
},
exportAll: function (exports, source) {
Object.keys(source).forEach(function (key) {
if (key === "default" || Object.prototype.hasOwnProperty.call(exports, key)) return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function () {
return source[key];
},
});
});
},
exponentiationOperator: function (a, b) {
return Math.pow(a, b);
},
initRegexNamedGroups: function () {},
getOriginalNativeErrorConstructor: function (name) {
return globalThis[name];
},
spawnAsync: function (generatorFunction, self, args) {
return new Promise(function (resolve, reject) {
var generator = generatorFunction.apply(self, args);
function step(resume) {
var next;
try {
next = resume();
} catch (e) {
reject(e);
return;
}
if (next.done) {
resolve(next.value);
return;
}
Promise.resolve(next.value).then(
function (value) {
step(function () {
return generator.next(value);
});
},
function (error) {
step(function () {
return generator["throw"](error);
});
}
);
}
step(function () {
return generator.next(undefined);
});
});
},
};
})();
if (typeof HermesInternal === "undefined") {
globalThis.HermesInternal = {
concat: function () {
return String.prototype.concat.apply(String(this), arguments);
},
};
}
// v96 generator bodies are native generators that start with a priming
// `yield` and receive {k, v} resumptions (k: 0 = next, 1 = return), so the
// compiled return/finally paths run exactly as in Hermes.
function $Delegated(result) {
this.result = result;
}
function $wrapGenerator(native) {
native.next();
var generator = Object.create($wrapGenerator.prototype);
function unwrap(step) {
return step.value instanceof $Delegated ? step.value.result : step;
}
generator.next = function (value) {
return unwrap(native.next({ k: 0, v: value }));
};
generator["return"] = function (value) {
return unwrap(native.next({ k: 1, v: value }));
};
generator["throw"] = function (error) {
return unwrap(native["throw"](error));
};
return generator;
}
$wrapGenerator.prototype[Symbol.iterator] = function () {
return this;
};
function $forInKeys(object) {
var keys = [];
if (object === null || object === undefined) return keys;
for (var key in object) keys.push(key);
return keys;
}
// v98 compiles generators to a resumable function called with
// (action, value) where action 0 = next, 1 = throw, 2 = return.
function $createGenerator(step) {
var generator = Object.create($createGenerator.prototype);
generator.next = function (value) {
return step.call(generator, 0, value);
};
generator["throw"] = function (value) {
return step.call(generator, 1, value);
};
generator["return"] = function (value) {
return step.call(generator, 2, value);
};
return generator;
}
$createGenerator.prototype[Symbol.iterator] = function () {
return this;
};
function $callWithNewTarget(callee, self, newTarget) {
var args = Array.prototype.slice.call(arguments, 3);
return newTarget === undefined ? callee.apply(self, args) : Reflect.construct(callee, args, newTarget);
}
// Closures whose environment is read directly by inlined code.
function $withEnv(closure, environment) {
Object.defineProperty(closure, "$env", { value: environment });
return closure;
}
function $deriveClass(constructor, superClass) {
Object.setPrototypeOf(constructor, superClass === null ? Function.prototype : superClass);
constructor.prototype = Object.create(superClass === null ? null : superClass.prototype, {
constructor: { value: constructor, writable: true, configurable: true },
});
return constructor;
}
export { HermesBuiltin, $Delegated, $wrapGenerator, $forInKeys, $createGenerator, $callWithNewTarget, $withEnv, $deriveClass };