Skip to content

Commit

Permalink
Use proto() util function consistently
Browse files Browse the repository at this point in the history
  • Loading branch information
TooTallNate committed Aug 27, 2024
1 parent 5db454d commit 7554cdf
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 41 deletions.
4 changes: 2 additions & 2 deletions packages/runtime/src/canvas/canvas-rendering-context-2d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
rgbaToString,
stub,
returnOnThrow,
proto,
} from '../utils';
import {
addIconFont,
Expand Down Expand Up @@ -49,8 +50,7 @@ export class CanvasRenderingContext2D {
constructor() {
assertInternalConstructor(arguments);
const canvas: Screen = arguments[1];
const ctx = $.canvasContext2dNew(canvas);
Object.setPrototypeOf(ctx, CanvasRenderingContext2D.prototype);
const ctx = proto($.canvasContext2dNew(canvas), CanvasRenderingContext2D);
_.set(ctx, { canvas });
ctx.font = '10px sans-serif';
return ctx;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
rgbaToString,
stub,
returnOnThrow,
proto,
} from '../utils';
import {
addIconFont,
Expand Down Expand Up @@ -49,8 +50,10 @@ export class OffscreenCanvasRenderingContext2D {
constructor() {
assertInternalConstructor(arguments);
const canvas: OffscreenCanvas = arguments[1];
const ctx = $.canvasContext2dNew(canvas);
Object.setPrototypeOf(ctx, OffscreenCanvasRenderingContext2D.prototype);
const ctx = proto(
$.canvasContext2dNew(canvas),
OffscreenCanvasRenderingContext2D,
);
_.set(ctx, { canvas });
ctx.font = '10px sans-serif';
return ctx;
Expand Down
7 changes: 3 additions & 4 deletions packages/runtime/src/canvas/offscreen-canvas.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { $ } from '../$';
import { Blob } from '../polyfills/blob';
import { EventTarget } from '../polyfills/event-target';
import { createInternal, def } from '../utils';
import { createInternal, def, proto } from '../utils';
import { OffscreenCanvasRenderingContext2D } from './offscreen-canvas-rendering-context-2d';
import type { ImageEncodeOptions } from '../types';
import { INTERNAL_SYMBOL } from '../internal';
Expand Down Expand Up @@ -39,10 +39,9 @@ export class OffscreenCanvas
*/
constructor(width: number, height: number) {
super();
const c = $.canvasNew(width, height) as OffscreenCanvas;
Object.setPrototypeOf(c, OffscreenCanvas.prototype);
const c = proto($.canvasNew(width, height), OffscreenCanvas);
_.set(c, {});
return c as OffscreenCanvas;
return c;
}

convertToBlob(options?: ImageEncodeOptions | undefined): Promise<Blob> {
Expand Down
5 changes: 2 additions & 3 deletions packages/runtime/src/font/font-face.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { $ } from '../$';
import { bufferSourceToArrayBuffer, def } from '../utils';
import { bufferSourceToArrayBuffer, def, proto } from '../utils';
import type {
FontFaceLoadStatus,
FontDisplay,
Expand Down Expand Up @@ -37,8 +37,7 @@ export class FontFace implements globalThis.FontFace {
throw new Error('Font `source` must be an ArrayBuffer');
}
const buffer = bufferSourceToArrayBuffer(source);
const f = $.fontFaceNew(buffer);
Object.setPrototypeOf(f, FontFace.prototype);
const f = proto($.fontFaceNew(buffer), FontFace);
f.family = family;
f.ascentOverride = descriptors.ascentOverride ?? 'normal';
f.descentOverride = descriptors.descentOverride ?? 'normal';
Expand Down
18 changes: 10 additions & 8 deletions packages/runtime/src/navigator/virtual-keyboard.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { $ } from '../$';
import { DOMRect } from '../domrect';
import { assertInternalConstructor, def } from '../utils';
import { assertInternalConstructor, def, proto } from '../utils';
import { Event } from '../polyfills/event';
import { EventTarget } from '../polyfills/event-target';
import { requestAnimationFrame, cancelAnimationFrame } from '../raf';
Expand Down Expand Up @@ -148,13 +148,15 @@ function onSubmit(this: VirtualKeyboard, str: string) {
}

export function create() {
const k = $.swkbdCreate({
onCancel,
onChange,
onCursorMove,
onSubmit,
});
Object.setPrototypeOf(k, VirtualKeyboard.prototype);
const k = proto(
$.swkbdCreate({
onCancel,
onChange,
onCursorMove,
onSubmit,
}),
VirtualKeyboard,
);
// @ts-expect-error
k.boundingRect = new DOMRect();
update = () => {
Expand Down
9 changes: 3 additions & 6 deletions packages/runtime/src/polyfills/url.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { $ } from '../$';
import { Blob } from './blob';
import { def, stub } from '../utils';
import { def, proto, stub } from '../utils';
import { crypto } from '../crypto';
import { inspect } from '../switch/inspect';

Expand All @@ -15,8 +15,7 @@ export class URLSearchParams implements globalThis.URLSearchParams {
if (init && (typeof init === 'string' || init instanceof URLSearchParams)) {
input = String(init);
}
const p = $.urlSearchNew(input, arguments[1]);
Object.setPrototypeOf(p, URLSearchParams.prototype);
const p = proto($.urlSearchNew(input, arguments[1]), URLSearchParams);
if (!input) {
if (Array.isArray(init)) {
for (const [k, v] of init) {
Expand Down Expand Up @@ -196,9 +195,7 @@ export class URL implements globalThis.URL {
* @param base The base URL to use in case the input URL is a relative URL.
*/
constructor(url: string | URL, base?: string | URL) {
const u = $.urlNew(url, base);
Object.setPrototypeOf(u, URL.prototype);
return u;
return proto($.urlNew(url, base), URL);
}

declare hash: string;
Expand Down
5 changes: 2 additions & 3 deletions packages/runtime/src/screen.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { $ } from './$';
import { assertInternalConstructor, createInternal, def } from './utils';
import { assertInternalConstructor, createInternal, def, proto } from './utils';
import { EventTarget } from './polyfills/event-target';
import { INTERNAL_SYMBOL } from './internal';
import { CanvasRenderingContext2D } from './canvas/canvas-rendering-context-2d';
Expand All @@ -19,8 +19,7 @@ export class Screen extends EventTarget implements globalThis.Screen {
constructor() {
assertInternalConstructor(arguments);
super();
const c = $.canvasNew(1280, 720) as Screen;
Object.setPrototypeOf(c, Screen.prototype);
const c = proto($.canvasNew(1280, 720), Screen);
_.set(c, {});
return c;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/runtime/src/tcp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
assertInternalConstructor,
bufferSourceToArrayBuffer,
createInternal,
proto,
toPromise,
} from './utils';
import type { BufferSource } from './types';
Expand Down Expand Up @@ -237,6 +238,5 @@ export function createServer(ip: string, port: number) {
});
server.dispatchEvent(new SocketEvent('accept', { socket }));
});
Object.setPrototypeOf(server, Server.prototype);
return server;
return proto(server, Server);
}
12 changes: 4 additions & 8 deletions packages/runtime/src/wasm.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { $ } from './$';
import { bufferSourceToArrayBuffer } from './utils';
import { bufferSourceToArrayBuffer, proto } from './utils';
import type { BufferSource } from './types';
import type {
WasmModuleOpaque,
Expand Down Expand Up @@ -174,11 +174,9 @@ function wrapExports(ex: any[]): Exports {
bindGlobal(g, v.val);
e[v.name] = g;
} else if (v.kind === 'memory') {
Object.setPrototypeOf(v.val, Memory.prototype);
e[v.name] = v.val;
e[v.name] = proto(v.val, Memory);
} else if (v.kind === 'table') {
Object.setPrototypeOf(v.val, Table.prototype);
e[v.name] = v.val;
e[v.name] = proto(v.val, Table);
} else {
throw new LinkError(`Unsupported export type "${v.kind}"`);
}
Expand Down Expand Up @@ -226,9 +224,7 @@ function callFunc(
*/
export class Memory implements WebAssembly.Memory {
constructor(descriptor: MemoryDescriptor) {
const that = $.wasmMemNew(descriptor);
Object.setPrototypeOf(that, Memory.prototype);
return that;
return proto($.wasmMemNew(descriptor), Memory);
}

/** [MDN Reference](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Memory/buffer) */
Expand Down
5 changes: 2 additions & 3 deletions packages/runtime/src/window.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assertInternalConstructor, def } from './utils';
import { assertInternalConstructor, def, proto } from './utils';
import { initKeyboard } from './keyboard';
import {
EventTarget,
Expand Down Expand Up @@ -28,8 +28,7 @@ export class Window extends EventTarget {
def(Window);

export var window: Window & typeof globalThis = globalThis;
Object.setPrototypeOf(window, Window.prototype);
def(window, 'window');
def(proto(window, Window), 'window');

Object.defineProperty(window, Symbol.toStringTag, {
get() {
Expand Down

0 comments on commit 7554cdf

Please sign in to comment.