Skip to content

Commit

Permalink
new super polyfills weeee
Browse files Browse the repository at this point in the history
  • Loading branch information
Arc-blroth committed Jun 23, 2023
1 parent 1918739 commit 7d2fa40
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 28 deletions.
3 changes: 2 additions & 1 deletion runtime/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,8 @@ pub mod not_docs {
"polyfills/runtime_01_version.js" as "01_version.ts",
"06_util.js",
"10_permissions.js",
"11_workers.js",
// minus_v8: we don't support workers
// "11_workers.js",
"12_io.js",
"13_buffer.js",
"30_fs.js",
Expand Down
50 changes: 25 additions & 25 deletions runtime/js/99_main.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import * as version from "internal:runtime/js/01_version.ts";
import * as os from "internal:runtime/js/30_os.js";
import * as colors from "internal:deno_console/01_colors.js";
import * as net from "internal:deno_net/01_net.js";
import * as performance from "internal:deno_web/15_performance.js";
// import * as performance from "internal:deno_web/15_performance.js";
import * as url from "internal:deno_url/00_url.js";
import { denoNs, denoNsUnstable } from "internal:runtime/js/90_deno_ns.js";
import { errors } from "internal:runtime/js/01_errors.js";
Expand Down Expand Up @@ -291,29 +291,29 @@ function bootstrapMainRuntime(runtimeOptions) {
// If the `--location` flag isn't set, make `globalThis.location` `undefined` and
// writable, so that they can mock it themselves if they like. If the flag was
// set, define `globalThis.location`, using the provided value.
if (runtimeOptions.location == null) {
mainRuntimeGlobalProperties.location = {
writable: true,
};
} else {
location.setLocationHref(runtimeOptions.location);
}
// if (runtimeOptions.location == null) {
// mainRuntimeGlobalProperties.location = {
// writable: true,
// };
// } else {
// location.setLocationHref(runtimeOptions.location);
// }

ObjectDefineProperties(globalThis, windowOrWorkerGlobalScope);
if (runtimeOptions.unstableFlag) {
ObjectDefineProperties(globalThis, unstableWindowOrWorkerGlobalScope);
}

event.setEventTargetData(globalThis);
event.saveGlobalThisReference(globalThis);
//event.setEventTargetData(globalThis);
//event.saveGlobalThisReference(globalThis);

event.defineEventHandler(globalThis, "error");
event.defineEventHandler(globalThis, "load");
event.defineEventHandler(globalThis, "beforeunload");
event.defineEventHandler(globalThis, "unload");
event.defineEventHandler(globalThis, "unhandledrejection");
//event.defineEventHandler(globalThis, "error");
//event.defineEventHandler(globalThis, "load");
//event.defineEventHandler(globalThis, "beforeunload");
//event.defineEventHandler(globalThis, "unload");
//event.defineEventHandler(globalThis, "unhandledrejection");

core.setPromiseRejectCallback(promiseRejectCallback);
// core.setPromiseRejectCallback(promiseRejectCallback);

const isUnloadDispatched = SymbolFor("isUnloadDispatched");
// Stores the flag for checking whether unload is dispatched or not.
Expand Down Expand Up @@ -386,7 +386,7 @@ function bootstrapWorkerRuntime(
}

core.initializeAsyncOps();
performance.setTimeOrigin(DateNow());
// performance.setTimeOrigin(DateNow());
globalThis_ = globalThis;

const consoleFromV8 = globalThis.Deno.core.console;
Expand Down Expand Up @@ -419,14 +419,14 @@ function bootstrapWorkerRuntime(
const consoleFromDeno = globalThis.console;
wrapConsole(consoleFromDeno, consoleFromV8);

event.setEventTargetData(globalThis);
event.saveGlobalThisReference(globalThis);
// event.setEventTargetData(globalThis);
// event.saveGlobalThisReference(globalThis);

event.defineEventHandler(self, "message");
event.defineEventHandler(self, "error", undefined, true);
event.defineEventHandler(self, "unhandledrejection");
// event.defineEventHandler(self, "message");
// event.defineEventHandler(self, "error", undefined, true);
// event.defineEventHandler(self, "unhandledrejection");

core.setPromiseRejectCallback(promiseRejectCallback);
// core.setPromiseRejectCallback(promiseRejectCallback);

// `Deno.exit()` is an alias to `self.close()`. Setting and exit
// code using an op in worker context is a no-op.
Expand All @@ -441,8 +441,8 @@ function bootstrapWorkerRuntime(

location.setLocationHref(runtimeOptions.location);

setNumCpus(runtimeOptions.cpuCount);
setLanguage(runtimeOptions.locale);
// setNumCpus(runtimeOptions.cpuCount);
// setLanguage(runtimeOptions.locale);

globalThis.pollForMessages = pollForMessages;

Expand Down
63 changes: 63 additions & 0 deletions runtime/js/polyfills/console_02_console.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const primordials = globalThis.__bootstrap.primordials;
const {
SymbolToStringTag,
ArrayPrototypeIncludes,
Proxy,
ReflectGet,
ReflectGetOwnPropertyDescriptor,
ReflectGetPrototypeOf,
} = primordials;

/** Creates a proxy that represents a subset of the properties
* of the original object optionally without evaluating the properties
* in order to get the values. */
function createFilteredInspectProxy({ object, keys, evaluate }) {
return new Proxy({}, {
get(_target, key) {
if (key === SymbolToStringTag) {
return object.constructor?.name;
} else if (ArrayPrototypeIncludes(keys, key)) {
return ReflectGet(object, key);
} else {
return undefined;
}
},
getOwnPropertyDescriptor(_target, key) {
if (!ArrayPrototypeIncludes(keys, key)) {
return undefined;
} else if (evaluate) {
return getEvaluatedDescriptor(object, key);
} else {
return getDescendantPropertyDescriptor(object, key) ??
getEvaluatedDescriptor(object, key);
}
},
has(_target, key) {
return ArrayPrototypeIncludes(keys, key);
},
ownKeys() {
return keys;
},
});

function getDescendantPropertyDescriptor(object, key) {
let propertyDescriptor = ReflectGetOwnPropertyDescriptor(object, key);
if (!propertyDescriptor) {
const prototype = ReflectGetPrototypeOf(object);
if (prototype) {
propertyDescriptor = getDescendantPropertyDescriptor(prototype, key);
}
}
return propertyDescriptor;
}

function getEvaluatedDescriptor(object, key) {
return {
configurable: true,
enumerable: true,
value: object[key],
};
}
}

export { createFilteredInspectProxy };
2 changes: 1 addition & 1 deletion runtime/js/polyfills/web_01_dom_exception.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
const DOMException = window.DOMException;
export { DOMException };
export default DOMException;
19 changes: 19 additions & 0 deletions runtime/js/polyfills/web_02_event.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const {
Symbol,
} = window.__bootstrap.primordials;

const _skipInternalInit = Symbol("[[skipSlowInit]]");
function setEventTargetData() {
// runtime should magically set up event target data by itself
}
Expand Down Expand Up @@ -95,7 +96,25 @@ function defineEventHandler(
});
}

const CloseEvent = window.CloseEvent;
const CustomEvent = window.CustomEvent;
const ErrorEvent = window.ErrorEvent;
const Event = window.Event;
const EventTarget = window.EventTarget;
const MessageEvent = window.MessageEvent;
const ProgressEvent = window.ProgressEvent;
const PromiseRejectionEvent = window.PromiseRejectionEvent;

export {
_skipInternalInit,
CloseEvent,
CustomEvent,
defineEventHandler,
ErrorEvent,
Event,
EventTarget,
MessageEvent,
ProgressEvent,
PromiseRejectionEvent,
setEventTargetData,
};
46 changes: 46 additions & 0 deletions runtime/js/polyfills/web_06_streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const {
Promise,
Proxy,
Symbol,
SymbolFor,
TypedArrayPrototypeSet,
Uint8ArrayPrototype,
} = globalThis.__bootstrap.primordials;
Expand Down Expand Up @@ -340,6 +341,50 @@ function errorReadableStream(stream, e) {
stream[_controller].error(e);
}

/**
* Create a new Writable object that is backed by a Resource that implements
* `Resource::write` / `Resource::write_all`. This object contains enough
* metadata to allow callers to bypass the JavaScript WritableStream
* implementation and write directly to the underlying resource if they so
* choose (FastStream).
*
* @param {number} rid The resource ID to write to.
* @param {boolean=} autoClose If the resource should be auto-closed when the stream closes. Defaults to true.
* @returns {ReadableStream<Uint8Array>}
*/
function writableStreamForRid(rid, autoClose = true) {
stream[_resourceBacking] = { rid, autoClose };

const tryClose = () => {
if (!autoClose) return;
RESOURCE_REGISTRY.unregister(stream);
core.tryClose(rid);
};

if (autoClose) {
RESOURCE_REGISTRY.register(stream, rid, stream);
}

const underlyingSink = {
async write(chunk, controller) {
try {
await core.writeAll(rid, chunk);
} catch (e) {
controller.error(e);
tryClose();
}
},
close() {
tryClose();
},
abort() {
tryClose();
},
};

return new WritableStream(underlyingSink);
}

/**
* @param {WritableStream} stream
* @returns {Promise<void>}
Expand Down Expand Up @@ -372,5 +417,6 @@ export {
ReadableStreamPrototype,
readableStreamThrowIfErrored,
WritableStream,
writableStreamForRid,
writableStreamClose,
};
5 changes: 4 additions & 1 deletion runtime/ops/polyfills.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ pub fn init() -> Vec<Extension> {
vec![
Extension::builder("deno_console")
.esm(
include_js_files!("../../ext/console/01_colors.js" as "01_colors.js",),
include_js_files!(
"../../ext/console/01_colors.js" as "01_colors.js",
"../js/polyfills/console_02_console.js" as "02_console.js",
),
)
.build(),
Extension::builder("deno_url")
Expand Down

0 comments on commit 7d2fa40

Please sign in to comment.