Skip to content

Commit

Permalink
chore: use eslint-config-reearth and format files
Browse files Browse the repository at this point in the history
  • Loading branch information
rot1024 committed Aug 26, 2022
1 parent 07bead9 commit af23b8d
Show file tree
Hide file tree
Showing 44 changed files with 987 additions and 838 deletions.
19 changes: 0 additions & 19 deletions .eslintrc

This file was deleted.

5 changes: 5 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
root: true
extends:
- reearth
rules:
'@typescript-eslint/ban-types': ['off']
13 changes: 2 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"src"
],
"engines": {
"node": ">=10"
"node": ">=12"
},
"scripts": {
"build": "tsc && vite build",
Expand All @@ -30,19 +30,10 @@
"peerDependencies": {
"quickjs-emscripten": "*"
},
"prettier": {
"printWidth": 80,
"semi": true,
"singleQuote": false,
"trailingComma": "es5"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^5.30.6",
"@typescript-eslint/parser": "^5.30.6",
"c8": "^7.11.3",
"eslint": "^8.19.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-prettier": "^4.2.1",
"eslint-config-reearth": "^0.1.0",
"prettier": "^2.7.1",
"quickjs-emscripten": "^0.21.0",
"typescript": "^4.7.4",
Expand Down
4 changes: 2 additions & 2 deletions src/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ export const defaultRegisteredObjects: [any, string][] = [
[URIError.prototype, "URIError.prototype"],
// built-in symbols
...Object.getOwnPropertyNames(Symbol)
.filter((k) => typeof (Symbol as any)[k] === "symbol")
.map<[any, string]>((k) => [(Symbol as any)[k], `Symbol.${k}`]),
.filter(k => typeof (Symbol as any)[k] === "symbol")
.map<[any, string]>(k => [(Symbol as any)[k], `Symbol.${k}`]),
];
21 changes: 8 additions & 13 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { getQuickJS } from "quickjs-emscripten";
import { describe, expect, test, vi } from "vitest";

import { Arena } from ".";
import { isWrapped } from "./wrapper";

import { Arena } from ".";

describe("readme", () => {
test("first", async () => {
class Cls {
Expand Down Expand Up @@ -89,7 +90,7 @@ describe("evalCode", () => {
get e() {
return { a: 1 };
}
})`
})`,
);
expect(result).toEqual({
a: 1,
Expand Down Expand Up @@ -180,19 +181,15 @@ describe("evalCode", () => {
const ctx = (await getQuickJS()).newContext();
const arena = new Arena(ctx, { isMarshalable: true });

const [promise, resolve] = arena.evalCode<
[Promise<string>, (d: string) => void]
>(`
const [promise, resolve] = arena.evalCode<[Promise<string>, (d: string) => void]>(`
let resolve;
const promise = new Promise(r => {
resolve = r;
}).then(d => d + "!");
[promise, resolve]
`);
expect(promise).instanceOf(Promise);
expect(isWrapped(arena._unwrapIfNotSynced(promise), arena._symbol)).toBe(
false
);
expect(isWrapped(arena._unwrapIfNotSynced(promise), arena._symbol)).toBe(false);

resolve("hoge");
expect(arena.executePendingJobs()).toBe(2);
Expand All @@ -207,7 +204,7 @@ describe("evalCode", () => {
const arena = new Arena(ctx, { isMarshalable: true });

const deferred: { resolve?: (s: string) => void } = {};
const promise = new Promise((resolve) => {
const promise = new Promise(resolve => {
deferred.resolve = resolve;
});
const res = vi.fn();
Expand Down Expand Up @@ -525,9 +522,7 @@ test("registeredObjects option", async () => {
});

expect(arena.evalCode(`Symbol.iterator`)).toBe(Symbol.iterator);
expect(arena.evalCode(`s => s === Symbol.iterator`)(Symbol.iterator)).toBe(
true
);
expect(arena.evalCode(`s => s === Symbol.iterator`)(Symbol.iterator)).toBe(true);

arena.dispose();
ctx.dispose();
Expand Down Expand Up @@ -567,7 +562,7 @@ describe("isMarshalable option", () => {
test("conditional", async () => {
const ctx = (await getQuickJS()).newContext();
const arena = new Arena(ctx, {
isMarshalable: (o) => o !== globalThis,
isMarshalable: o => o !== globalThis,
});

const obj = { a: 1 };
Expand Down
67 changes: 19 additions & 48 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,13 @@ import type {
VmCallResult,
} from "quickjs-emscripten";

import VMMap from "./vmmap";
import { defaultRegisteredObjects } from "./default";
import marshal from "./marshal";
import unmarshal from "./unmarshal";
import { wrap, wrapHandle, unwrap, unwrapHandle, Wrapped } from "./wrapper";
import { complexity, isES2015Class, isObject, walkObject } from "./util";
import {
call,
eq,
isHandleObject,
json,
consumeAll,
mayConsume,
handleFrom,
} from "./vmutil";
import { defaultRegisteredObjects } from "./default";
import VMMap from "./vmmap";
import { call, eq, isHandleObject, json, consumeAll, mayConsume, handleFrom } from "./vmutil";
import { wrap, wrapHandle, unwrap, unwrapHandle, Wrapped } from "./wrapper";

export {
VMMap,
Expand All @@ -47,13 +39,9 @@ export type Options = {
*/
registeredObjects?: Iterable<[any, QuickJSHandle | string]>;
/** Register functions to convert an object to a QuickJS handle. */
customMarshaller?: Iterable<
(target: unknown, ctx: QuickJSContext) => QuickJSHandle | undefined
>;
customMarshaller?: Iterable<(target: unknown, ctx: QuickJSContext) => QuickJSHandle | undefined>;
/** Register functions to convert a QuickJS handle to an object. */
customUnmarshaller?: Iterable<
(target: QuickJSHandle, ctx: QuickJSContext) => any
>;
customUnmarshaller?: Iterable<(target: QuickJSHandle, ctx: QuickJSContext) => any>;
/** A callback that returns a boolean value that determines whether an object is wrappable by proxies. If returns false, note that the object cannot be synchronized between the host and the QuickJS even if arena.sync is used. */
isWrappable?: (target: any) => boolean;
/** A callback that returns a boolean value that determines whether an QuickJS handle is wrappable by proxies. If returns false, note that the handle cannot be synchronized between the host and the QuickJS even if arena.sync is used. */
Expand Down Expand Up @@ -130,7 +118,7 @@ export class Arena {
*/
expose(obj: { [k: string]: any }) {
for (const [key, value] of Object.entries(obj)) {
mayConsume(this._marshal(value), (handle) => {
mayConsume(this._marshal(value), handle => {
this.context.setProp(this.context.global, key, handle);
});
}
Expand All @@ -144,7 +132,7 @@ export class Arena {
sync<T>(target: T): T {
const wrapped = this._wrap(target);
if (typeof wrapped === "undefined") return target;
walkObject(wrapped, (v) => {
walkObject(wrapped, v => {
const u = this._unwrap(v);
this._sync.add(u);
});
Expand Down Expand Up @@ -182,10 +170,7 @@ export class Arena {
* Unregister a pair of objects that were registered with `registeredObjects` option and `register` method.
*/
unregister(target: any, dispose?: boolean) {
this._registeredMap.delete(
target,
this._registeredMapDispose.has(target) || dispose
);
this._registeredMap.delete(target, this._registeredMapDispose.has(target) || dispose);
this._registeredMapDispose.delete(target);
}

Expand Down Expand Up @@ -215,13 +200,9 @@ export class Arena {
throw this._unwrapIfNotSynced(result.error.consume(this._unmarshal));
}

_unwrapResultAndUnmarshal(
result: VmCallResult<QuickJSHandle> | undefined
): any {
_unwrapResultAndUnmarshal(result: VmCallResult<QuickJSHandle> | undefined): any {
if (!result) return;
return this._unwrapIfNotSynced(
this._unwrapResult(result).consume(this._unmarshal)
);
return this._unwrapIfNotSynced(this._unwrapResult(result).consume(this._unmarshal));
}

_isMarshalable = (t: unknown): boolean | "json" => {
Expand All @@ -242,17 +223,13 @@ export class Arena {
_marshalPre = (
t: unknown,
h: QuickJSHandle | QuickJSDeferredPromise,
mode: true | "json" | undefined
mode: true | "json" | undefined,
): Wrapped<QuickJSHandle> | undefined => {
if (mode === "json") return;
return this._register(t, handleFrom(h), this._map)?.[1];
};

_marshalPreApply = (
target: Function,
that: unknown,
args: unknown[]
): void => {
_marshalPreApply = (target: Function, that: unknown, args: unknown[]): void => {
const unwrapped = isObject(that) ? this._unwrap(that) : undefined;
// override sync mode of this object while calling the function
if (unwrapped) this._temporalSync.add(unwrapped);
Expand Down Expand Up @@ -311,7 +288,7 @@ export class Arena {
t: any,
h: QuickJSHandle,
map: VMMap = this._map,
sync?: boolean
sync?: boolean,
): [Wrapped<any>, Wrapped<QuickJSHandle>] | undefined {
if (this._registeredMap.has(t) || this._registeredMap.hasHandle(h)) {
return;
Expand Down Expand Up @@ -340,9 +317,7 @@ export class Arena {

_syncMode = (obj: any): "both" | undefined => {
const obj2 = this._unwrap(obj);
return this._sync.has(obj2) || this._temporalSync.has(obj2)
? "both"
: undefined;
return this._sync.has(obj2) || this._temporalSync.has(obj2) ? "both" : undefined;
};

_wrap<T>(target: T): Wrapped<T> | undefined {
Expand All @@ -353,7 +328,7 @@ export class Arena {
this._symbolHandle,
this._marshal,
this._syncMode,
this._options?.isWrappable
this._options?.isWrappable,
);
}

Expand All @@ -363,22 +338,18 @@ export class Arena {

_unwrapIfNotSynced = <T>(target: T): T => {
const unwrapped = this._unwrap(target);
return unwrapped instanceof Promise || !this._sync.has(unwrapped)
? unwrapped
: target;
return unwrapped instanceof Promise || !this._sync.has(unwrapped) ? unwrapped : target;
};

_wrapHandle(
handle: QuickJSHandle
): [Wrapped<QuickJSHandle> | undefined, boolean] {
_wrapHandle(handle: QuickJSHandle): [Wrapped<QuickJSHandle> | undefined, boolean] {
return wrapHandle(
this.context,
handle,
this._symbol,
this._symbolHandle,
this._unmarshal,
this._syncMode,
this._options?.isHandleWrappable
this._options?.isHandleWrappable,
);
}

Expand Down
9 changes: 3 additions & 6 deletions src/marshal/custom.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { getQuickJS } from "quickjs-emscripten";
import { expect, test, vi } from "vitest";

import { call } from "../vmutil";

import marshalCustom, { defaultCustom } from "./custom";
Expand Down Expand Up @@ -38,12 +39,8 @@ test("date", async () => {

const handle = marshal(date);
if (!handle) throw new Error("handle is undefined");
expect(ctx.dump(call(ctx, "d => d instanceof Date", undefined, handle))).toBe(
true
);
expect(ctx.dump(call(ctx, "d => d.getTime()", undefined, handle))).toBe(
date.getTime()
);
expect(ctx.dump(call(ctx, "d => d instanceof Date", undefined, handle))).toBe(true);
expect(ctx.dump(call(ctx, "d => d.getTime()", undefined, handle))).toBe(date.getTime());
expect(pre).toReturnTimes(1);
expect(pre.mock.calls[0][0]).toBe(date);
expect(pre.mock.calls[0][1] === handle).toBe(true);
Expand Down
28 changes: 6 additions & 22 deletions src/marshal/custom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,8 @@ import { call } from "../vmutil";
export default function marshalCustom(
ctx: QuickJSContext,
target: unknown,
preMarshal: (
target: unknown,
handle: QuickJSHandle
) => QuickJSHandle | undefined,
custom: Iterable<
(target: unknown, ctx: QuickJSContext) => QuickJSHandle | undefined
>
preMarshal: (target: unknown, handle: QuickJSHandle) => QuickJSHandle | undefined,
custom: Iterable<(target: unknown, ctx: QuickJSContext) => QuickJSHandle | undefined>,
): QuickJSHandle | undefined {
let handle: QuickJSHandle | undefined;
for (const c of custom) {
Expand All @@ -21,31 +16,20 @@ export default function marshalCustom(
return handle ? preMarshal(target, handle) ?? handle : undefined;
}

export function symbol(
target: unknown,
ctx: QuickJSContext
): QuickJSHandle | undefined {
export function symbol(target: unknown, ctx: QuickJSContext): QuickJSHandle | undefined {
if (typeof target !== "symbol") return;
const handle = call(
ctx,
"d => Symbol(d)",
undefined,
target.description ? ctx.newString(target.description) : ctx.undefined
target.description ? ctx.newString(target.description) : ctx.undefined,
);
return handle;
}

export function date(
target: unknown,
ctx: QuickJSContext
): QuickJSHandle | undefined {
export function date(target: unknown, ctx: QuickJSContext): QuickJSHandle | undefined {
if (!(target instanceof Date)) return;
const handle = call(
ctx,
"d => new Date(d)",
undefined,
ctx.newNumber(target.getTime())
);
const handle = call(ctx, "d => new Date(d)", undefined, ctx.newNumber(target.getTime()));
return handle;
}

Expand Down
Loading

0 comments on commit af23b8d

Please sign in to comment.