Skip to content

Commit

Permalink
fix boxing/unboxing
Browse files Browse the repository at this point in the history
  • Loading branch information
sparecycles committed Oct 11, 2024
1 parent 5926675 commit 6cb701c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/core/schema/core/debugging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ governing permissions and limitations under the License.
*/

// turn this on to capture stack traces into schemas and template objects.
export const DEBUG = false;
export const DEBUG = true;
7 changes: 4 additions & 3 deletions packages/core/src/core/schema/definition/datum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ function defineScalar<T extends Scalar>(self: DatumRepresentation): Schema<T> {
}

if (appraised !== undefined) {
appraised = convertScalar(appraised, type) as T;
appraised = convertScalar(appraised, type, { unboxed }) as T;
}

if (appraised === undefined && context.mode === "match") {
Expand Down Expand Up @@ -454,7 +454,7 @@ function resolveScalar<T extends Scalar>(
throw diagnostic(context, `define: ${issue}`);
}

return convertScalar(result, self.type) as T;
return convertScalar(result, self.type, { unboxed }) as T;
}
}

Expand All @@ -478,14 +478,15 @@ async function doRenderScalar<T>(
// if there's a pattern which is already defined, we can evaluate it
const definition = resolveDefinedPattern(context, configuredPatterns);
if (definition !== undefined) {
result = convertScalar(definition, type);
result = convertScalar(definition, type, { unboxed });
}

if (mode === "render" || mode === "prerender" || mode === "postrender") {
if (result === undefined) {
result = convertScalar(
(await evaluateScalar(context, configuredPatterns)) as Scalar,
type,
{ unboxed },
);
}

Expand Down
14 changes: 11 additions & 3 deletions packages/core/src/core/schema/definition/scalar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export type ScalarType = "string" | "number" | "boolean" | "bigint" | "null";
export function convertScalar(
value?: Scalar | unknown,
type?: ScalarType,
anull?: boolean,
{ anull, unboxed }: { anull?: boolean; unboxed?: boolean } = {},
): Scalar | unknown {
if (value === undefined) {
return anull ? null : undefined;
Expand All @@ -52,7 +52,7 @@ export function convertScalar(
return createNumber(value["source"]);
}

return createNumber(String(value));
return unboxed ? Number(value) : createNumber(String(value));
case "string":
return String(value);
case "bigint":
Expand All @@ -64,7 +64,11 @@ export function convertScalar(
return value;
}

return value !== undefined ? createBigInt(String(value)) : undefined;
return value !== undefined
? unboxed
? BigInt(String(value))
: createBigInt(String(value))
: undefined;
default:
return value;
}
Expand Down Expand Up @@ -113,11 +117,15 @@ export function createNumber(source: string, value?: number) {
const numberObject = Object.assign(new Number(value ?? source), { source });

if (DEBUG) {
const created = new Error("created-at");
Object.defineProperty(numberObject, "valueOf", {
value() {
return Number(value ?? source);
},
});
Object.defineProperty(numberObject, "created", {
value: created,
});
}

Object.defineProperty(numberObject, "toString", {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ export function defineReference<T = unknown>(
// if the schema was a stub, we'll try to derive the value
// by evaluating the refs.
if (result !== undefined) {
return convertScalar(result, encoding, anull) as T;
return convertScalar(result, encoding, { anull }) as T;
}
}

Expand All @@ -368,7 +368,7 @@ export function defineReference<T = unknown>(

defineReferenceValue(context, value);

return convertScalar(value, encoding, anull) as T;
return convertScalar(value, encoding, { anull }) as T;
}

return anull ? (null as T) : undefined!;
Expand Down

0 comments on commit 6cb701c

Please sign in to comment.