From 4d2973cff7ec05c807716d7033385a73c996934b Mon Sep 17 00:00:00 2001 From: Christoph Knittel Date: Thu, 3 Dec 2026 19:19:15 +0000 Subject: [PATCH] Pass the constant arguments of Dict and Object functions without @as(json) Why: A prototype for deprecating `@as(json)` on external arguments. The seven stdlib uses become an external that takes the constant plus an `@inline` function of the same name that passes `{}` or `null`, the way `Array.reduce` reorders its arguments. Calls compile to the same JavaScript as before. JS unchanged (call sites): Example: let merged = target->Dict.concat(dict{"b": 3}) let bare = Object.createWithNull() Before (js): let merged = Object.assign({}, target, { b: 3 }); let bare = Object.create(null); After (js): let merged = Object.assign({}, target, { b: 3 }); let bare = Object.create(null); Review: What changes for users? `Stdlib_Dict.js` and `Stdlib_Object.js` now export these functions, as `Stdlib_Array.js` exports `reduce`. A function value such as `Dict.copy` passed around calls the exported function instead of a wrapper made at the use site. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Kbf3gHZYwA1rq7zbk3dhQq Signed-off-by: Christoph Knittel --- changes/stdlib-no-as-json-args.internal.md | 1 + packages/@rescript/runtime/Stdlib_Dict.res | 16 ++++++++++---- packages/@rescript/runtime/Stdlib_Dict.resi | 12 ++++------ packages/@rescript/runtime/Stdlib_Object.res | 18 ++++++++++++--- .../@rescript/runtime/lib/es6/Stdlib_Dict.mjs | 20 +++++++++++++++++ .../runtime/lib/es6/Stdlib_Object.mjs | 22 ++++++++++++++++++- .../@rescript/runtime/lib/js/Stdlib_Dict.cjs | 20 +++++++++++++++++ .../runtime/lib/js/Stdlib_Object.cjs | 20 ++++++++++++++++- 8 files changed, 112 insertions(+), 17 deletions(-) create mode 100644 changes/stdlib-no-as-json-args.internal.md diff --git a/changes/stdlib-no-as-json-args.internal.md b/changes/stdlib-no-as-json-args.internal.md new file mode 100644 index 00000000000..8ac0b269b11 --- /dev/null +++ b/changes/stdlib-no-as-json-args.internal.md @@ -0,0 +1 @@ +- Pass the constant arguments of `Dict.concat`, `Dict.concatMany`, `Dict.concatAll`, `Dict.copy`, `Object.copy`, `Object.createWithNull` and `Object.createWithNullAndProperties` from `@inline` functions instead of `@as(json)`. diff --git a/packages/@rescript/runtime/Stdlib_Dict.res b/packages/@rescript/runtime/Stdlib_Dict.res index 0383829759f..82bc53bc22b 100644 --- a/packages/@rescript/runtime/Stdlib_Dict.res +++ b/packages/@rescript/runtime/Stdlib_Dict.res @@ -27,14 +27,22 @@ external fromIterable: Stdlib_Iterable.t<(string, 'a)> => dict<'a> = "Object.fro @variadic @val external assignMany: (dict<'a>, array>) => dict<'a> = "Object.assign" -@val external concat: (@as(json`{}`) _, dict<'a>, dict<'a>) => dict<'a> = "Object.assign" +@val external concat: (dict<'a>, dict<'a>, dict<'a>) => dict<'a> = "Object.assign" + +@inline +let concat = (dict1, dict2) => concat(make(), dict1, dict2) @variadic @val -external concatMany: (@as(json`{}`) _, dict<'a>, array>) => dict<'a> = "Object.assign" +external concatMany: (dict<'a>, dict<'a>, array>) => dict<'a> = "Object.assign" + +@inline +let concatMany = (target, sources) => concatMany(make(), target, sources) -@variadic @val external concatAll: (@as(json`{}`) _, array>) => dict<'a> = "Object.assign" +@inline +let concatAll = dictionaries => assignMany(make(), dictionaries) -@val external copy: (@as(json`{}`) _, dict<'a>) => dict<'a> = "Object.assign" +@inline +let copy = dictionary => assign(make(), dictionary) // Use %raw to support for..in which is a ~10% faster than .forEach let forEach: (dict<'a>, 'a => unit) => unit = %raw(`(dict, f) => { diff --git a/packages/@rescript/runtime/Stdlib_Dict.resi b/packages/@rescript/runtime/Stdlib_Dict.resi index 224be3ffefb..b156ca36fcd 100644 --- a/packages/@rescript/runtime/Stdlib_Dict.resi +++ b/packages/@rescript/runtime/Stdlib_Dict.resi @@ -279,8 +279,7 @@ merged == dict{"firstKey": 2, "someKey": 3} (merged === dict1) == false ``` */ -@val -external concat: (@as(json`{}`) _, dict<'a>, dict<'a>) => dict<'a> = "Object.assign" +let concat: (dict<'a>, dict<'a>) => dict<'a> /** `concatMany(target, sources)` [shallowly](https://developer.mozilla.org/en-US/docs/Glossary/Shallow_copy) merges `target` and each dictionary in `sources` into a fresh dictionary, and returns the new dictionary. @@ -300,8 +299,7 @@ merged == dict{"firstKey": 1, "someKey": 3, "someKey2": 4} (merged === target) == false ``` */ -@variadic @val -external concatMany: (@as(json`{}`) _, dict<'a>, array>) => dict<'a> = "Object.assign" +let concatMany: (dict<'a>, array>) => dict<'a> /** `concatAll(dictionaries)` [shallowly](https://developer.mozilla.org/en-US/docs/Glossary/Shallow_copy) merges all dictionaries in `dictionaries` into a fresh dictionary, and returns the new dictionary. @@ -321,8 +319,7 @@ merged == dict{"firstKey": 1, "someKey": 3, "someKey2": 4} (merged === dict1) == false ``` */ -@variadic @val -external concatAll: (@as(json`{}`) _, array>) => dict<'a> = "Object.assign" +let concatAll: array> => dict<'a> /** `copy(dictionary)` [shallowly copies](https://developer.mozilla.org/en-US/docs/Glossary/Shallow_copy) the provided dictionary to a new dictionary. @@ -336,8 +333,7 @@ let dict2 = dict->Dict.copy Console.log2(dict->Dict.keysToArray, dict2->Dict.keysToArray) ``` */ -@val -external copy: (@as(json`{}`) _, dict<'a>) => dict<'a> = "Object.assign" +let copy: dict<'a> => dict<'a> /** `forEach(dictionary, f)` iterates through all values of the dict. diff --git a/packages/@rescript/runtime/Stdlib_Object.res b/packages/@rescript/runtime/Stdlib_Object.res index 27a314a0d38..afb4957011e 100644 --- a/packages/@rescript/runtime/Stdlib_Object.res +++ b/packages/@rescript/runtime/Stdlib_Object.res @@ -73,6 +73,8 @@ obj->Object.get("kind") == Some("fruit") */ @val external createWithProperties: ({..}, {..}) => {..} = "Object.create" +@val external createWithNull: Primitive_js_extern.null<{..}> => {..} = "Object.create" + /** `createWithNull()` creates an object with a `null` prototype (no inherited properties). @@ -85,7 +87,12 @@ let obj = Object.createWithNull() obj->Object.get("toString") == None ``` */ -@val external createWithNull: (@as(json`null`) _, unit) => {..} = "Object.create" +@inline +let createWithNull = () => createWithNull(Primitive_js_extern.null) + +@val +external createWithNullAndProperties: (Primitive_js_extern.null<{..}>, {..}) => {..} = + "Object.create" /** `createWithNullAndProperties(descriptors)` creates an object with a `null` prototype and defines properties using descriptor objects. @@ -100,7 +107,9 @@ obj->Object.get("name") == Some("banana") obj->Object.get("toString") == None ``` */ -@val external createWithNullAndProperties: (@as(json`null`) _, {..}) => {..} = "Object.create" +@inline +let createWithNullAndProperties = properties => + createWithNullAndProperties(Primitive_js_extern.null, properties) /** `assign(target, source)` copies enumerable own properties from the source to the target, overwriting properties with the same name. It returns the modified target object. A deep clone is not created; properties are copied by reference. @@ -130,6 +139,8 @@ See [Object.assign on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScri @variadic @val external assignMany: ({..}, array<{..}>) => {..} = "Object.assign" +@val external copy: ({..}, {..} as 'a) => 'a = "Object.assign" + /** `copy(object)` creates a shallow copy of `object`. @@ -144,7 +155,8 @@ cloned->Object.get("name") == Some("banana") Object.is(original, cloned) == false ``` */ -@val external copy: (@as(json`{}`) _, {..} as 'a) => 'a = "Object.assign" +@inline +let copy = object => copy(make(), object) /** `get` gets the value of a property by name. Returns `None` if the property does not exist or has the value `undefined`. Otherwise returns `Some`, including if the value is `null`. diff --git a/packages/@rescript/runtime/lib/es6/Stdlib_Dict.mjs b/packages/@rescript/runtime/lib/es6/Stdlib_Dict.mjs index c57b1e1fb0d..9ac7f025897 100644 --- a/packages/@rescript/runtime/lib/es6/Stdlib_Dict.mjs +++ b/packages/@rescript/runtime/lib/es6/Stdlib_Dict.mjs @@ -5,6 +5,22 @@ function $$delete$1(dict, string) { delete(dict[string]); } +function concat(dict1, dict2) { + return Object.assign({}, dict1, dict2); +} + +function concatMany(target, sources) { + return Object.assign({}, target, ...sources); +} + +function concatAll(dictionaries) { + return Object.assign({}, ...dictionaries); +} + +function copy(dictionary) { + return Object.assign({}, dictionary); +} + let forEach = ((dict, f) => { for (var i in dict) { f(dict[i]); @@ -44,6 +60,10 @@ export { $$delete$1 as $$delete, size, isEmpty, + concat, + concatMany, + concatAll, + copy, forEach, forEachWithKey, mapValues, diff --git a/packages/@rescript/runtime/lib/es6/Stdlib_Object.mjs b/packages/@rescript/runtime/lib/es6/Stdlib_Object.mjs index ae1b9f17e65..02aa3a13d0d 100644 --- a/packages/@rescript/runtime/lib/es6/Stdlib_Object.mjs +++ b/packages/@rescript/runtime/lib/es6/Stdlib_Object.mjs @@ -1 +1,21 @@ -/* This output is empty. Its source's type definitions, externals and/or unused code got optimized away. */ + + + +function createWithNull() { + return Object.create(null); +} + +function createWithNullAndProperties(properties) { + return Object.create(null, properties); +} + +function copy(object) { + return Object.assign({}, object); +} + +export { + createWithNull, + createWithNullAndProperties, + copy, +} +/* No side effect */ diff --git a/packages/@rescript/runtime/lib/js/Stdlib_Dict.cjs b/packages/@rescript/runtime/lib/js/Stdlib_Dict.cjs index b38f7a9c40e..0bab6e25f6c 100644 --- a/packages/@rescript/runtime/lib/js/Stdlib_Dict.cjs +++ b/packages/@rescript/runtime/lib/js/Stdlib_Dict.cjs @@ -5,6 +5,22 @@ function $$delete$1(dict, string) { delete(dict[string]); } +function concat(dict1, dict2) { + return Object.assign({}, dict1, dict2); +} + +function concatMany(target, sources) { + return Object.assign({}, target, ...sources); +} + +function concatAll(dictionaries) { + return Object.assign({}, ...dictionaries); +} + +function copy(dictionary) { + return Object.assign({}, dictionary); +} + let forEach = ((dict, f) => { for (var i in dict) { f(dict[i]); @@ -43,6 +59,10 @@ let isEmpty = ((dict) => { exports.$$delete = $$delete$1; exports.size = size; exports.isEmpty = isEmpty; +exports.concat = concat; +exports.concatMany = concatMany; +exports.concatAll = concatAll; +exports.copy = copy; exports.forEach = forEach; exports.forEachWithKey = forEachWithKey; exports.mapValues = mapValues; diff --git a/packages/@rescript/runtime/lib/js/Stdlib_Object.cjs b/packages/@rescript/runtime/lib/js/Stdlib_Object.cjs index ae1b9f17e65..4976350a450 100644 --- a/packages/@rescript/runtime/lib/js/Stdlib_Object.cjs +++ b/packages/@rescript/runtime/lib/js/Stdlib_Object.cjs @@ -1 +1,19 @@ -/* This output is empty. Its source's type definitions, externals and/or unused code got optimized away. */ +'use strict'; + + +function createWithNull() { + return Object.create(null); +} + +function createWithNullAndProperties(properties) { + return Object.create(null, properties); +} + +function copy(object) { + return Object.assign({}, object); +} + +exports.createWithNull = createWithNull; +exports.createWithNullAndProperties = createWithNullAndProperties; +exports.copy = copy; +/* No side effect */