Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/stdlib-inline-helpers.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Mark small stdlib functions `@inline`, so calls to them compile to the code they run: argument-reordering wrappers such as `Array.reduce` (which now compiles to `array.reduce(f, init)`), `Promise.catch` and the iterator and generator `next` functions; the `Null`, `Nullable` and `Result` combinators such as `map` and `getOr`; the `…Opt` lookups of `Array` and `String`; and predicates such as `isEmpty`.
1 change: 1 addition & 0 deletions changes/stdlib-int-constants.breaking.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- `Int.Constants.minValue` and `maxValue` are now `@inline` constants, so ReScript code using them gets the number itself. JavaScript code that reads them from the runtime's `Stdlib_Int` module gets `undefined`.
12 changes: 12 additions & 0 deletions packages/@rescript/runtime/Dom_storage.res
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
type t = Dom_storage2.t

@send @return(null_to_opt) external getItem: (t, string) => option<string> = "getItem"

@inline
let getItem = (s, obj) => obj->getItem(s)

/* https://developer.mozilla.org/en-US/docs/Web/API/Storage/getItem
If the key does not exist, `null` is returned
*/

@send external setItem: (t, string, string) => unit = "setItem"

@inline
let setItem = (k, v, obj): unit => obj->setItem(k, v)

@send external removeItem: (t, string) => unit = "removeItem"

@inline
let removeItem = (s, obj): unit => obj->removeItem(s)

@send external clear: t => unit = "clear"
@send @return(null_to_opt) external key: (t, int) => option<string> = "key"

/* A DOMString containing the name of the key. If the index does not exist, null is returned.
If the key does not exist, `null` is returned
*/
@inline
let key = (i, obj): option<string> => obj->key(i)

@get external length: t => int = "length"

@val external localStorage: t = "localStorage"
Expand Down
15 changes: 15 additions & 0 deletions packages/@rescript/runtime/Stdlib_Array.res
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ let fromInitializer = (~length, f) =>

external length: array<'a> => int = "%array_length"

@inline
let isEmpty = arr => arr->length === 0

let rec equalFromIndex = (a, b, i, eq, len) =>
Expand Down Expand Up @@ -118,6 +119,8 @@ external removeInPlace: (array<'a>, int, @as(1) _) => unit = "splice"

@send external concat: (array<'a>, array<'a>) => array<'a> = "concat"
@variadic @send external concatMany: (array<'a>, array<array<'a>>) => array<'a> = "concat"

@inline
let concatAll = arrays => concatMany([], arrays)

@send external flat: array<array<'a>> => array<'a> = "flat"
Expand All @@ -126,6 +129,7 @@ let concatAll = arrays => concatMany([], arrays)

@send external indexOf: (array<'a>, 'a, ~from: int=?) => int = "indexOf"

@inline
let indexOfOpt = (arr, item) =>
switch arr->indexOf(item) {
| -1 => None
Expand All @@ -138,6 +142,7 @@ let indexOfOpt = (arr, item) =>

@send external lastIndexOf: (array<'a>, 'a, ~from: int=?) => int = "lastIndexOf"

@inline
let lastIndexOfOpt = (arr, item) =>
switch arr->lastIndexOf(item) {
| -1 => None
Expand Down Expand Up @@ -179,18 +184,25 @@ let lastIndexOfOpt = (arr, item) =>
@send external mapWithIndex: (array<'a>, ('a, int) => 'b) => array<'b> = "map"

@send external reduce: (array<'b>, ('a, 'b) => 'a, 'a) => 'a = "reduce"

@inline
let reduce = (arr, init, f) => reduce(arr, f, init)

@send external reduceWithIndex: (array<'b>, ('a, 'b, int) => 'a, 'a) => 'a = "reduce"

@inline
let reduceWithIndex = (arr, init, f) => reduceWithIndex(arr, f, init)

@send
external reduceRight: (array<'b>, ('a, 'b) => 'a, 'a) => 'a = "reduceRight"

@inline
let reduceRight = (arr, init, f) => reduceRight(arr, f, init)

@send
external reduceRightWithIndex: (array<'b>, ('a, 'b, int) => 'a, 'a) => 'a = "reduceRight"

@inline
let reduceRightWithIndex = (arr, init, f) => reduceRightWithIndex(arr, f, init)

@send external some: (array<'a>, 'a => bool) => bool = "some"
Expand All @@ -203,12 +215,14 @@ let reduceRightWithIndex = (arr, init, f) => reduceRightWithIndex(arr, f, init)
@get_index external getSymbolUnsafe: (array<'a>, Stdlib_Symbol.t) => 'b = ""
@set_index external setSymbol: (array<'a>, Stdlib_Symbol.t, 'b) => unit = ""

@inline
let findIndexOpt = (array: array<'a>, finder: 'a => bool): option<int> =>
switch findIndex(array, finder) {
| -1 => None
| index => Some(index)
}

@inline
let findLastIndexOpt = (array: array<'a>, finder: 'a => bool): option<int> =>
switch findLastIndex(array, finder) {
| -1 => None
Expand Down Expand Up @@ -352,6 +366,7 @@ let unzip = a => {

@send external at: (array<'a>, int) => option<'a> = "at"

@inline
let last = a => a->get(a->length - 1)

external ignore: array<'a> => unit = "%ignore"
Expand Down
2 changes: 2 additions & 0 deletions packages/@rescript/runtime/Stdlib_AsyncGenerator.res
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ external asAsyncIterator: t<'yield, 'return, 'next> => Stdlib_AsyncIterator.t<
'next,
> = "%identity"

@inline
let next = generator => generator->asAsyncIterator->Stdlib_AsyncIterator.next

@inline
let nextValue = (generator, value) =>
generator->asAsyncIterator->Stdlib_AsyncIterator.nextValue(value)

Expand Down
2 changes: 2 additions & 0 deletions packages/@rescript/runtime/Stdlib_AsyncIterableIterator.res
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ external asAsyncIterator: t<'yield, 'return, 'next> => Stdlib_AsyncIterator.t<
'next,
> = "%identity"

@inline
let next = iterator => iterator->asAsyncIterator->Stdlib_AsyncIterator.next

@inline
let nextValue = (iterator, value) =>
iterator->asAsyncIterator->Stdlib_AsyncIterator.nextValue(value)

Expand Down
1 change: 1 addition & 0 deletions packages/@rescript/runtime/Stdlib_BigInt.res
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ external toLocaleString: bigint => string = "toLocaleString"

@val external toFloat: bigint => float = "Number"

@inline
let toInt = t => t->toFloat->Stdlib_Int.fromFloat

external add: (bigint, bigint) => bigint = "%addbigint"
Expand Down
1 change: 1 addition & 0 deletions packages/@rescript/runtime/Stdlib_Bool.res
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
type t = bool

@inline
let toString = b =>
if b {
"true"
Expand Down
2 changes: 2 additions & 0 deletions packages/@rescript/runtime/Stdlib_Date.res
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,10 @@ module UTC = {
@send external getTime: t => msSinceEpoch = "getTime"
@send external getTimezoneOffset: t => int = "getTimezoneOffset"

@inline
let equal = (a, b) => a->getTime === b->getTime

@inline
let compare = (a, b) => Stdlib_Float.compare(a->getTime, b->getTime)

// Locale
Expand Down
1 change: 1 addition & 0 deletions packages/@rescript/runtime/Stdlib_Dict.res
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ type t<'a> = dict<'a>
@set_index external set: (dict<'a>, string, 'a) => unit = ""
@val external delete: 'a => unit = "delete"

@inline
let delete = (dict, string) => {
delete(get(dict, string))
}
Expand Down
4 changes: 4 additions & 0 deletions packages/@rescript/runtime/Stdlib_Generator.res
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ external asIteratorObject: t<'yield, 'return, 'next> => Stdlib_IteratorObject.t<
'next,
> = "%identity"

@inline
let next = generator => generator->asIterator->Stdlib_Iterator.next

@inline
let nextValue = (generator, value) => generator->asIterator->Stdlib_Iterator.nextValue(value)

@send
Expand All @@ -21,6 +23,7 @@ external returnValueRaw: (
'return,
) => Stdlib_Iterator.rawResult<'yield, 'return> = "return"

@inline
let returnValue = (generator, value) =>
generator->returnValueRaw(value)->Stdlib_Iterator.normalizeResult

Expand All @@ -30,5 +33,6 @@ external throwErrorRaw: (
exn,
) => Stdlib_Iterator.rawResult<'yield, 'return> = "throw"

@inline
let throwError = (generator, error) =>
generator->throwErrorRaw(error)->Stdlib_Iterator.normalizeResult
2 changes: 2 additions & 0 deletions packages/@rescript/runtime/Stdlib_Int.resi
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ on MDN.
Console.log(Int.Constants.minValue)
```
*/
@inline(-2147483648)
let minValue: int

/**
Expand All @@ -39,6 +40,7 @@ on MDN.
Console.log(Int.Constants.maxValue)
```
*/
@inline(2147483647)
let maxValue: int
}

Expand Down
2 changes: 2 additions & 0 deletions packages/@rescript/runtime/Stdlib_IterableIterator.res
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ external asIterator: t<'yield, 'return, 'next> => Stdlib_Iterator.t<'yield, 'ret

external asIterable: t<'yield, 'return, 'next> => Stdlib_Iterable.t<'yield> = "%identity"

@inline
let next = iterator => iterator->asIterator->Stdlib_Iterator.next

@inline
let nextValue = (iterator, value) => iterator->asIterator->Stdlib_Iterator.nextValue(value)

@val
Expand Down
2 changes: 2 additions & 0 deletions packages/@rescript/runtime/Stdlib_IteratorObject.res
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ external asIterator: t<'yield, 'return, 'next> => Stdlib_Iterator.t<'yield, 'ret

external asIterable: t<'yield, 'return, 'next> => Stdlib_Iterable.t<'yield> = "%identity"

@inline
let next = iteratorObject => iteratorObject->asIterator->Stdlib_Iterator.next

@inline
let nextValue = (iteratorObject, value) =>
iteratorObject->asIterator->Stdlib_Iterator.nextValue(value)

Expand Down
3 changes: 3 additions & 0 deletions packages/@rescript/runtime/Stdlib_List.res
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ external mutableCell: ('a, list<'a>) => list<'a> = "#makemutablelist"
- the cell is not empty
*/

@inline
let head = x =>
switch x {
| list{} => None
Expand All @@ -94,6 +95,7 @@ let headOrThrow = x =>
| list{x, ..._} => x
}

@inline
let tail = x =>
switch x {
| list{} => None
Expand All @@ -106,6 +108,7 @@ let tailOrThrow = x =>
| list{_, ...t} => t
}

@inline
let add = (xs, x) => list{x, ...xs}

/* Assume `n >=0` */
Expand Down
1 change: 1 addition & 0 deletions packages/@rescript/runtime/Stdlib_Map.res
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ external fromIterable: Stdlib_Iterable.t<('k, 'v)> => t<'k, 'v> = "Map"

@get external size: t<'k, 'v> => int = "size"

@inline
let isEmpty = map => map->size === 0

@send external clear: t<'k, 'v> => unit = "clear"
Expand Down
6 changes: 6 additions & 0 deletions packages/@rescript/runtime/Stdlib_Null.res
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ external make: 'a => t<'a> = "%identity"

external toOption: t<'a> => option<'a> = "#null_to_opt"

@inline
let fromOption: option<'a> => t<'a> = option =>
switch option {
| Some(x) => make(x)
Expand All @@ -21,6 +22,7 @@ let equal = (a, b, eq) => Stdlib_Option.equal(a->toOption, b->toOption, eq)

let compare = (a, b, cmp) => Stdlib_Option.compare(a->toOption, b->toOption, cmp)

@inline
let getOr = (value, default) =>
switch value->toOption {
| Some(x) => x
Expand All @@ -35,24 +37,28 @@ let getOrThrow: t<'a> => 'a = value =>

external getUnsafe: t<'a> => 'a = "%identity"

@inline
let forEach = (value, f) =>
switch value->toOption {
| Some(x) => f(x)
| None => ()
}

@inline
let map = (value, f) =>
switch value->toOption {
| Some(x) => make(f(x))
| None => null
}

@inline
let mapOr = (value, default, f) =>
switch value->toOption {
| Some(x) => f(x)
| None => default
}

@inline
let flatMap = (value, f) =>
switch value->toOption {
| Some(x) => f(x)
Expand Down
6 changes: 6 additions & 0 deletions packages/@rescript/runtime/Stdlib_Nullable.res
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ external make: 'a => t<'a> = "%identity"

external toOption: t<'a> => option<'a> = "#nullable_to_opt"

@inline
let fromOption: option<'a> => t<'a> = option =>
switch option {
| Some(x) => make(x)
Expand All @@ -24,6 +25,7 @@ let equal = (a, b, eq) => Stdlib_Option.equal(a->toOption, b->toOption, eq)

let compare = (a, b, cmp) => Stdlib_Option.compare(a->toOption, b->toOption, cmp)

@inline
let getOr = (value, default) =>
switch value->toOption {
| Some(x) => x
Expand All @@ -38,24 +40,28 @@ let getOrThrow: t<'a> => 'a = value =>

external getUnsafe: t<'a> => 'a = "%identity"

@inline
let forEach = (value, f) =>
switch value->toOption {
| Some(x) => f(x)
| None => ()
}

@inline
let map = (value, f) =>
switch value->toOption {
| Some(x) => make(f(x))
| None => Obj.magic(value)
}

@inline
let mapOr = (value, default, f) =>
switch value->toOption {
| Some(x) => f(x)
| None => default
}

@inline
let flatMap = (value, f) =>
switch value->toOption {
| Some(x) => f(x)
Expand Down
Loading
Loading