-- JavaScript that runs while a value is being converted. -- -- Converting a JavaScript value can run the value's own code -- a getter, -- valueOf(), toString(), toJSON() -- and converting a PostgreSQL value used -- to run code the value never asked for, a setter it inherited. -- 1) An exception thrown during a conversion is the error. Most paths -- reported "could not convert JavaScript value to a number" or "... to a -- string" instead; float8 and numeric became NaN, oid became 0, a jsonb -- property became "", an array whose length threw became {} and a fallback -- type's {is_null} getter became SQL NULL, all silently; and json crashed the -- backend. CREATE TYPE cuc_pair AS (a int4, b int4); CREATE FUNCTION cuc_record(OUT a int4, OUT b int4) LANGUAGE pljs AS $$ return { get a() { throw new Error('record getter'); }, b: 1 }; $$; SELECT * FROM cuc_record(); ERROR: record getter DETAIL: Error: record getter at get a (:3:37) CREATE FUNCTION cuc_int4() RETURNS int4 LANGUAGE pljs AS $$ return { valueOf() { throw new Error('int4 valueOf'); } }; $$; SELECT cuc_int4(); ERROR: int4 valueOf DETAIL: Error: int4 valueOf at valueOf (:3:39) CREATE FUNCTION cuc_float8() RETURNS float8 LANGUAGE pljs AS $$ return { valueOf() { throw new Error('float8 valueOf'); } }; $$; SELECT cuc_float8(); ERROR: float8 valueOf DETAIL: Error: float8 valueOf at valueOf (:3:39) CREATE FUNCTION cuc_numeric() RETURNS numeric LANGUAGE pljs AS $$ return { valueOf() { throw new Error('numeric valueOf'); } }; $$; SELECT cuc_numeric(); ERROR: numeric valueOf DETAIL: Error: numeric valueOf at valueOf (:3:39) CREATE FUNCTION cuc_oid() RETURNS oid LANGUAGE pljs AS $$ return { valueOf() { throw new Error('oid valueOf'); } }; $$; SELECT cuc_oid(); ERROR: oid valueOf DETAIL: Error: oid valueOf at valueOf (:3:39) CREATE FUNCTION cuc_text() RETURNS text LANGUAGE pljs AS $$ return { toString() { throw new Error('text toString'); } }; $$; SELECT cuc_text(); ERROR: text toString DETAIL: Error: text toString at toString (:3:40) CREATE FUNCTION cuc_uuid() RETURNS uuid LANGUAGE pljs AS $$ return { toString() { throw new Error('uuid toString'); } }; $$; SELECT cuc_uuid(); ERROR: uuid toString DETAIL: Error: uuid toString at toString (:3:40) CREATE FUNCTION cuc_uuid_is_null() RETURNS uuid LANGUAGE pljs AS $$ return { get is_null() { throw new Error('is_null getter'); } }; $$; SELECT cuc_uuid_is_null(); ERROR: is_null getter DETAIL: Error: is_null getter at get is_null (:3:43) CREATE FUNCTION cuc_json() RETURNS json LANGUAGE pljs AS $$ return { toJSON() { throw new Error('json toJSON'); } }; $$; SELECT cuc_json(); ERROR: json toJSON DETAIL: Error: json toJSON at toJSON (:3:38) CREATE FUNCTION cuc_jsonb() RETURNS jsonb LANGUAGE pljs AS $$ return { get a() { throw new Error('jsonb getter'); } }; $$; SELECT cuc_jsonb(); ERROR: jsonb getter DETAIL: Error: jsonb getter at get a (:3:37) CREATE FUNCTION cuc_array_element() RETURNS int4[] LANGUAGE pljs AS $$ const a = [1, 2]; Object.defineProperty(a, 1, { get() { throw new Error('element getter'); } }); return a; $$; SELECT cuc_array_element(); ERROR: element getter DETAIL: Error: element getter at get (:4:56) CREATE FUNCTION cuc_array_length() RETURNS int4[] LANGUAGE pljs AS $$ return new Proxy([1, 2], { get(target, key) { if (key === 'length') { throw new Error('length getter'); } return target[key]; } }); $$; SELECT cuc_array_length(); ERROR: length getter DETAIL: Error: length getter at get (:6:24) CREATE FUNCTION cuc_date() RETURNS timestamptz LANGUAGE pljs AS $$ const d = new Date(1000); d.valueOf = () => { throw new Error('date valueOf'); }; return d; $$; SELECT cuc_date(); ERROR: date valueOf DETAIL: Error: date valueOf at (:4:38) at [Symbol.toPrimitive] (native) -- A Date converts through its own Symbol.toPrimitive when it has one. It -- has to be defined: Date.prototype's is read-only, so assigning one does -- nothing, as in any JavaScript engine. CREATE FUNCTION cuc_date_to_primitive() RETURNS timestamptz LANGUAGE pljs AS $$ const d = new Date(1000); Object.defineProperty(d, Symbol.toPrimitive, { value: () => { throw new Error('date toPrimitive'); } }); return d; $$; SELECT cuc_date_to_primitive(); ERROR: date toPrimitive DETAIL: Error: date toPrimitive at value (:5:35) -- A PostgreSQL error raised from a getter keeps its SQLSTATE. CREATE FUNCTION cuc_getter_sql_error(OUT a int4, OUT b int4) LANGUAGE pljs AS $$ return { get a() { return pljs.execute('SELECT 1 / 0 AS x')[0].x; }, b: 1 }; $$; SELECT * FROM cuc_getter_sql_error(); ERROR: division by zero DETAIL: Error: division by zero at get a (:3:41) \echo :LAST_ERROR_SQLSTATE 22012 -- A bound parameter's error reaches the JavaScript that bound it. DO $$ try { pljs.execute('SELECT $1::int4 AS x', [{ valueOf() { throw new Error('param valueOf'); } }]); } catch (e) { pljs.elog(NOTICE, 'caught: ' + e.message); } $$ LANGUAGE pljs; NOTICE: caught: param valueOf -- 2) Rows a single-column set returns take the same forms return_next() -- accepts. `[{a: 7}]` converted the object itself as the int4. CREATE FUNCTION cuc_rows() RETURNS TABLE (a int4) LANGUAGE pljs AS $$ return [{ a: 7 }, { get a() { return 8; } }, { b: 9 }, 10, { valueOf() { return 11; } }]; $$; SELECT * FROM cuc_rows(); a ---- 7 8 9 10 11 (5 rows) CREATE FUNCTION cuc_row() RETURNS TABLE (a int4) LANGUAGE pljs AS $$ return { a: 12 }; $$; SELECT * FROM cuc_row(); a ---- 12 (1 row) CREATE FUNCTION cuc_row_ambiguous() RETURNS TABLE (a int4) LANGUAGE pljs AS $$ return [{ x: 1, y: 2 }]; $$; SELECT * FROM cuc_row_ambiguous(); ERROR: returned row: object does not identify a value for the result column "a" (property names are case sensitive; a single-column set also accepts the bare value) -- A json column still takes the object as its value. CREATE FUNCTION cuc_json_rows() RETURNS TABLE (j jsonb) LANGUAGE pljs AS $$ return [{ j: 1 }, { x: 2 }]; $$; SELECT * FROM cuc_json_rows(); j ---------- {"j": 1} {"x": 2} (2 rows) -- 3) Objects and arrays built from PostgreSQL values define their properties -- rather than set them. Setting ran setters inherited from Object.prototype -- and Array.prototype -- during argument conversion, before the call had an -- SPI connection -- and the setter took the value: here the array's first -- element and the SPI result's first row, which made rows[0] undefined. A -- column named __proto__ replaced the row object's prototype instead of -- becoming a property. CREATE FUNCTION cuc_install() RETURNS void LANGUAGE pljs AS $$ globalThis.cuc_hits = ''; Object.defineProperty(Object.prototype, 'a', { set(v) { globalThis.cuc_hits += 'a=' + v + ';'; }, configurable: true }); Object.defineProperty(Array.prototype, 0, { set(v) { globalThis.cuc_hits += '[0]=' + v + ';'; }, configurable: true }); $$; CREATE FUNCTION cuc_uninstall() RETURNS text LANGUAGE pljs AS $$ delete Object.prototype.a; delete Array.prototype[0]; return globalThis.cuc_hits; $$; CREATE FUNCTION cuc_values(r cuc_pair, xs int4[], j jsonb) RETURNS text LANGUAGE pljs AS $$ const rows = pljs.execute('SELECT 5 AS a'); const hits = globalThis.cuc_hits; return [r.a, xs[0], xs.length, j.a, j.list[0], rows[0].a, 'hits: ' + hits].join(' '); $$; SELECT cuc_install(); cuc_install ------------- (1 row) SELECT cuc_values((7, 1)::cuc_pair, ARRAY[1, 2], '{"a": 3, "list": [4]}'); cuc_values -------------------- 7 1 2 3 4 5 hits: (1 row) SELECT cuc_uninstall() = '' AS no_setter_ran; no_setter_ran --------------- t (1 row) CREATE FUNCTION cuc_proto() RETURNS text LANGUAGE pljs AS $$ const r = pljs.execute('SELECT 1 AS "__proto__", 2 AS x')[0]; return JSON.stringify([Object.keys(r), Object.getPrototypeOf(r) === Object.prototype]); $$; SELECT cuc_proto(); cuc_proto -------------------------- [["__proto__","x"],true] (1 row) -- 4) new Date(0) is a Date. The brand check read the Date's time value as a -- pointer, and the epoch's is zero, so it was parsed from its toString(), -- which fails outside UTC. CREATE FUNCTION cuc_epoch() RETURNS TABLE (d date, t timestamptz) LANGUAGE pljs AS $$ return [{ d: new Date(0), t: new Date(0) }]; $$; SELECT * FROM cuc_epoch(); d | t ------------+------------------------------ 01-01-1970 | Wed Dec 31 16:00:00 1969 PST (1 row) -- 5) A Date in jsonb is written as JSON.stringify() writes it. Every Date -- was stored as {}, and json already wrote the ISO string. CREATE FUNCTION cuc_jsonb_dates() RETURNS TABLE (j jsonb, s json) LANGUAGE pljs AS $$ const v = { epoch: new Date(0), before: new Date(-1500), list: [new Date(Date.UTC(2020, 1, 29, 12, 34, 56, 789))], invalid: new Date(NaN) }; return [{ j: v, s: v }]; $$; SELECT * FROM cuc_jsonb_dates(); j | s ------------------------------------------------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------- {"list": ["2020-02-29T12:34:56.789Z"], "epoch": "1970-01-01T00:00:00.000Z", "before": "1969-12-31T23:59:58.500Z", "invalid": null} | {"epoch":"1970-01-01T00:00:00.000Z","before":"1969-12-31T23:59:58.500Z","list":["2020-02-29T12:34:56.789Z"],"invalid":null} (1 row) CREATE FUNCTION cuc_jsonb_date() RETURNS jsonb LANGUAGE pljs AS $$ return new Date(1000); $$; SELECT cuc_jsonb_date(); cuc_jsonb_date ---------------------------- "1970-01-01T00:00:01.000Z" (1 row) DROP FUNCTION cuc_record, cuc_int4, cuc_float8, cuc_numeric, cuc_oid, cuc_text, cuc_uuid, cuc_uuid_is_null, cuc_json, cuc_jsonb, cuc_array_element, cuc_array_length, cuc_date, cuc_date_to_primitive, cuc_epoch, cuc_jsonb_dates, cuc_jsonb_date, cuc_getter_sql_error, cuc_rows, cuc_row, cuc_row_ambiguous, cuc_json_rows, cuc_install, cuc_uninstall, cuc_values, cuc_proto; DROP TYPE cuc_pair;