Skip to content

Commit

Permalink
refactor global constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdbradley committed Oct 4, 2021
1 parent 09ed37b commit 9bf6961
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 29 deletions.
44 changes: 22 additions & 22 deletions src/lib/sandbox/read-main-interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
import { getConstructorName, isValidMemberName, noop } from '../utils';
import { InterfaceInfo, InterfaceType } from '../types';
import { InterfaceInfo, InterfaceType, MembersInterfaceTypeInfo } from '../types';

export const readMainInterfaces = (win: Window, doc: Document) => {
const docImpl = doc.implementation.createHTMLDocument();
const docHead = docImpl.head;

const implementations: [InterfaceType, any][] = [
const implementations: MainImplementation[] = [
[InterfaceType.Window, win],
[InterfaceType.Element, docHead],
[InterfaceType.TextNode, docImpl.createTextNode('')],
[InterfaceType.DocumentFragmentNode, docImpl.createDocumentFragment()],
[InterfaceType.CSSStyleDeclaration, docHead.style],
[InterfaceType.Document, docImpl],
[InterfaceType.DocumentFragmentNode, docImpl.createDocumentFragment()],
[InterfaceType.DOMStringMap, docHead.dataset],
[InterfaceType.DOMTokenList, docHead.classList],
[InterfaceType.Element, docHead],
[InterfaceType.History, win.history],
[InterfaceType.NodeList, docHead.childNodes],
[InterfaceType.Storage, win.sessionStorage],
[InterfaceType.MutationObserver, new MutationObserver(noop)],
];
[InterfaceType.NamedNodeMap, docHead.attributes],
[InterfaceType.NodeList, docHead.childNodes],
[InterfaceType.Storage, win.localStorage],
[InterfaceType.TextNode, docImpl.createTextNode('')],
].map((i) => [...i, getConstructorName(i[1] as any)]) as any;

return implementations.map(([interfaceType, impl]) => {
let members: any = {};
return implementations.map(([interfaceType, impl, cstrName]) => {
let members: MembersInterfaceTypeInfo = {};
let memberName: string;
let value: any;
let type: string;
let objInterfaceType: any;
let objCstrName: string;
let objImpl: MainImplementation | undefined;

for (memberName in impl) {
if (isValidMemberName(memberName)) {
Expand All @@ -32,23 +36,19 @@ export const readMainInterfaces = (win: Window, doc: Document) => {
if (type === 'function') {
members[memberName] = InterfaceType.Function;
} else if (type === 'object') {
objInterfaceType = InterfaceWhitelist[getConstructorName(value)];
if (objInterfaceType) {
members[memberName] = objInterfaceType;
objCstrName = getConstructorName(value);
objImpl = implementations.find((i) => i[2] === objCstrName);
if (objImpl) {
// this object's constructor is one of the interfaces we care about
members[memberName] = objImpl[0];
}
}
}
}

const interfaceInfo: InterfaceInfo = [interfaceType, members, getConstructorName(impl)];
const interfaceInfo: InterfaceInfo = [interfaceType, cstrName, members];
return interfaceInfo;
});
};

const InterfaceWhitelist: { [key: string]: InterfaceType } = {
CSSStyleDeclaration: InterfaceType.CSSStyleDeclaration,
DOMStringMap: InterfaceType.DOMStringMap,
DOMTokenList: InterfaceType.DOMTokenList,
NamedNodeMap: InterfaceType.NamedNodeMap,
NodeList: InterfaceType.NodeList,
};
type MainImplementation = [InterfaceType, any, string];
4 changes: 2 additions & 2 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ export interface InitWebWorkerContext {
$postMessage$: (msg: MessageFromWorkerToSandbox) => void;
}

export type InterfaceInfo = [InterfaceType, MemberTypeInfo, string];
export type InterfaceInfo = [InterfaceType, string, MembersInterfaceTypeInfo];

export interface MemberTypeInfo {
export interface MembersInterfaceTypeInfo {
[memberName: string]: InterfaceType;
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib/web-worker/init-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const initWebWorker = (self: Worker, initWebWorkerData: InitWebWorkerData

initWebWorkerGlobal(
self,
initWebWorkerData.$interfaces$[0][1],
initWebWorkerData.$interfaces$[0][2],
initWebWorkerData.$interfaces$,
initWebWorkerData.$htmlConstructors$
);
Expand Down
11 changes: 8 additions & 3 deletions src/lib/web-worker/worker-global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@ import { HTMLIFrameElement, Window } from './worker-iframe';
import { HTMLImageElement } from './worker-image';
import { HTMLScriptElement } from './worker-script';
import { InstanceIdKey, webWorkerCtx, WinIdKey } from './worker-constants';
import { InterfaceInfo, InterfaceType, MemberTypeInfo, PlatformInstanceId } from '../types';
import {
InterfaceInfo,
InterfaceType,
MembersInterfaceTypeInfo,
PlatformInstanceId,
} from '../types';
import { nextTick, TOP_WIN_ID } from '../utils';
import { Node } from './worker-node';
import { sendBeacon } from './worker-exec';

export const initWebWorkerGlobal = (
self: any,
windowMemberTypeInfo: MemberTypeInfo,
windowMemberTypeInfo: MembersInterfaceTypeInfo,
interfaces: InterfaceInfo[],
htmlCstrNames: string[]
) => {
Expand All @@ -31,7 +36,7 @@ export const initWebWorkerGlobal = (
}
});

interfaces.map((i) => createGlobalConstructorProxy(self, i[0], i[2]));
interfaces.map((i) => createGlobalConstructorProxy(self, i[0], i[1]));

self.requestAnimationFrame = (cb: (t: number) => void) => nextTick(() => cb(Date.now()), 9);
self.cancelAnimationFrame = clearTimeout;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/web-worker/worker-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ const createComplexMember = (

const interfaceInfo = webWorkerCtx.$interfaces$.find((i) => i[0] === interfaceType);
if (interfaceInfo) {
const memberTypeInfo = interfaceInfo[1];
const memberTypeInfo = interfaceInfo[2];
const memberInfo = memberTypeInfo[memberPath[len(memberPath) - 1]];
if (memberInfo === InterfaceType.Function) {
return (...args: any[]) => callMethod(instance, memberPath, args);
Expand Down

1 comment on commit 9bf6961

@vercel
Copy link

@vercel vercel bot commented on 9bf6961 Oct 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.