feat: support bigint in evaluates (#23930)

Fixes #22719.
This commit is contained in:
Dmitry Gozman 2023-06-28 08:55:45 -07:00 committed by GitHub
parent c6a0f3e8a0
commit b0b429fed0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 18 additions and 0 deletions

View file

@ -71,6 +71,8 @@ function innerParseSerializedValue(value: SerializedValue, handles: any[] | unde
return new Date(value.d); return new Date(value.d);
if (value.u !== undefined) if (value.u !== undefined)
return new URL(value.u); return new URL(value.u);
if (value.bi !== undefined)
return BigInt(value.bi);
if (value.r !== undefined) if (value.r !== undefined)
return new RegExp(value.r.p, value.r.f); return new RegExp(value.r.p, value.r.f);
@ -133,6 +135,8 @@ function innerSerializeValue(value: any, handleSerializer: (value: any) => Handl
return { n: value }; return { n: value };
if (typeof value === 'string') if (typeof value === 'string')
return { s: value }; return { s: value };
if (typeof value === 'bigint')
return { bi: value.toString() };
if (isError(value)) { if (isError(value)) {
const error = value; const error = value;
if ('captureStackTrace' in globalThis.Error) { if ('captureStackTrace' in globalThis.Error) {

View file

@ -57,6 +57,7 @@ scheme.SerializedValue = tObject({
v: tOptional(tEnum(['null', 'undefined', 'NaN', 'Infinity', '-Infinity', '-0'])), v: tOptional(tEnum(['null', 'undefined', 'NaN', 'Infinity', '-Infinity', '-0'])),
d: tOptional(tString), d: tOptional(tString),
u: tOptional(tString), u: tOptional(tString),
bi: tOptional(tString),
r: tOptional(tObject({ r: tOptional(tObject({
p: tString, p: tString,
f: tString, f: tString,

View file

@ -19,6 +19,7 @@ export type SerializedValue =
{ v: 'null' | 'undefined' | 'NaN' | 'Infinity' | '-Infinity' | '-0' } | { v: 'null' | 'undefined' | 'NaN' | 'Infinity' | '-Infinity' | '-0' } |
{ d: string } | { d: string } |
{ u: string } | { u: string } |
{ bi: string } |
{ r: { p: string, f: string} } | { r: { p: string, f: string} } |
{ a: SerializedValue[], id: number } | { a: SerializedValue[], id: number } |
{ o: { k: string, v: SerializedValue }[], id: number } | { o: { k: string, v: SerializedValue }[], id: number } |
@ -91,6 +92,8 @@ export function source() {
return new Date(value.d); return new Date(value.d);
if ('u' in value) if ('u' in value)
return new URL(value.u); return new URL(value.u);
if ('bi' in value)
return BigInt(value.bi);
if ('r' in value) if ('r' in value)
return new RegExp(value.r.p, value.r.f); return new RegExp(value.r.p, value.r.f);
if ('a' in value) { if ('a' in value) {
@ -157,6 +160,8 @@ export function source() {
return value; return value;
if (typeof value === 'string') if (typeof value === 'string')
return value; return value;
if (typeof value === 'bigint')
return { bi: value.toString() };
if (isError(value)) { if (isError(value)) {
const error = value; const error = value;

View file

@ -179,6 +179,7 @@ export type SerializedValue = {
v?: 'null' | 'undefined' | 'NaN' | 'Infinity' | '-Infinity' | '-0', v?: 'null' | 'undefined' | 'NaN' | 'Infinity' | '-Infinity' | '-0',
d?: string, d?: string,
u?: string, u?: string,
bi?: string,
r?: { r?: {
p: string, p: string,
f: string, f: string,

View file

@ -80,6 +80,8 @@ SerializedValue:
d: string? d: string?
# String representation of the URL. # String representation of the URL.
u: string? u: string?
# String representation of BigInt.
bi: string?
# Regular expression pattern and flags. # Regular expression pattern and flags.
r: r:
type: object? type: object?

View file

@ -94,6 +94,11 @@ it('should transfer arrays as arrays, not objects', async ({ page }) => {
expect(result).toBe(true); expect(result).toBe(true);
}); });
it('should transfer bigint', async ({ page }) => {
expect(await page.evaluate(() => 42n)).toBe(42n);
expect(await page.evaluate(a => a, 17n)).toBe(17n);
});
it('should transfer maps as empty objects', async ({ page }) => { it('should transfer maps as empty objects', async ({ page }) => {
const result = await page.evaluate(a => a.x.constructor.name + ' ' + JSON.stringify(a.x), { x: new Map([[1, 2]]) }); const result = await page.evaluate(a => a.x.constructor.name + ' ' + JSON.stringify(a.x), { x: new Map([[1, 2]]) });
expect(result).toBe('Object {}'); expect(result).toBe('Object {}');