Files
ask262/test/engine262/WeakRef.test.mts
T
bendtherules 6e92d2345e Squashed 'engine262/' content from commit ae71998
git-subtree-dir: engine262
git-subtree-split: ae71998cc5a8315700555135b1ac202a0d6d0b31
2026-03-27 10:07:29 +05:30

78 lines
2.4 KiB
TypeScript

import { expect, test } from 'vitest';
import {
Agent, evalQ, Get, isPromiseObject, JSStringValue, ManagedRealm, NormalCompletion, setSurroundingAgent,
skipDebugger,
ThrowCompletion,
unwrapCompletion,
Value,
type PromiseObject,
} from '#self';
test('WeakRef (script)', () => {
const agent = new Agent();
setSurroundingAgent(agent);
const realm = new ManagedRealm();
const result = realm.evaluateScript(`
const w = new WeakRef({});
Promise.resolve()
.then(() => {
if (typeof w.deref() !== 'object') {
throw new Error();
}
})
.then(() => {
if (typeof w.deref() !== 'undefined') {
throw new Error();
}
})
.then(() => 'pass');
`) as NormalCompletion<PromiseObject>;
expect(result).toBeInstanceOf(NormalCompletion);
expect(isPromiseObject(result.Value)).toBe(true);
expect(result.Value.PromiseState).toBe('fulfilled');
if (!(result.Value.PromiseResult instanceof JSStringValue)) {
throw new Error('Expected JSStringValue');
}
expect(result.Value.PromiseResult.stringValue()).toBe('pass');
});
test('WeakRef (module)', () => {
const agent = new Agent();
setSurroundingAgent(agent);
const realm = new ManagedRealm();
realm.scope(() => {
const module = realm.compileModule(`
const w = new WeakRef({});
globalThis.result = Promise.resolve()
.then(() => {
if (typeof w.deref() !== 'object') {
throw new Error('should be object');
}
})
.then(() => {
if (typeof w.deref() !== 'undefined') {
throw new Error('should be undefined');
}
})
.then(() => 'pass');
`, { specifier: 'test.mjs' });
if (module instanceof ThrowCompletion) {
throw new Error('Module compilation failed');
}
const completion = evalQ((_Q, X) => {
const m = X(module);
m.LoadRequestedModules();
X(m.Link());
skipDebugger(m.Evaluate());
const result = X(skipDebugger(Get(realm.GlobalObject, Value('result')))) as PromiseObject;
expect(isPromiseObject(result)).toBe(true);
expect(result.PromiseState).toBe('fulfilled');
if (!(result.PromiseResult instanceof JSStringValue)) {
throw new Error('Expected JSStringValue');
}
expect(result.PromiseResult.stringValue()).toBe('pass');
});
unwrapCompletion(completion);
});
});