From 426e92ee9872d67661ac6e35548b7e555b6aba4b Mon Sep 17 00:00:00 2001 From: Peter Saxton Date: Mon, 10 Jun 2024 16:08:24 +0200 Subject: rename ffi file --- CHANGELOG.md | 4 + src/ffi.mjs | 167 ------------------------------------- src/gleam/javascript.gleam | 12 +-- src/gleam/javascript/array.gleam | 12 +-- src/gleam/javascript/map.gleam | 8 +- src/gleam/javascript/promise.gleam | 38 ++++----- src/gleam_javascript_ffi.mjs | 167 +++++++++++++++++++++++++++++++++++++ 7 files changed, 206 insertions(+), 202 deletions(-) delete mode 100644 src/ffi.mjs create mode 100644 src/gleam_javascript_ffi.mjs diff --git a/CHANGELOG.md b/CHANGELOG.md index 0c976fd..56c1673 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## Unreleased + +- Renamed ffi file to be scope with project name. + ## v0.10.0 - 2024-06-23 - The `Reference` type has been deprecated. diff --git a/src/ffi.mjs b/src/ffi.mjs deleted file mode 100644 index 7c14c10..0000000 --- a/src/ffi.mjs +++ /dev/null @@ -1,167 +0,0 @@ -import { Ok, Error } from "./gleam.mjs"; -import { - UndefinedType, - ObjectType, - BooleanType, - NumberType, - BigIntType, - StringType, - SymbolType, - FunctionType, -} from "./gleam/javascript.mjs"; - -export function toArray(list) { - return list.toArray(); -} - -export function map(thing, fn) { - return thing.map(fn); -} - -export function length(thing) { - return thing.length; -} - -export function reduce(thing, acc, fn) { - return thing.reduce(fn, acc); -} - -export function reduceRight(thing, acc, fn) { - return thing.reduceRight(fn, acc); -} - -export function index(thing, index) { - return index in thing ? new Ok(thing[index]) : new Error(undefined); -} - -export function object_from_entries(entries) { - return Object.fromEntries(entries); -} - -export function type_of(value) { - switch (typeof value) { - case "undefined": - return new UndefinedType(); - case "object": - return new ObjectType(); - case "boolean": - return new BooleanType(); - case "number": - return new NumberType(); - case "bigint": - return new BigIntType(); - case "string": - return new StringType(); - case "symbol": - return new SymbolType(); - case "function": - return new FunctionType(); - default: - throw new globalThis.Error(`Unexpected typeof ${typeof value}`); - } -} - -export function get_symbol(name) { - return Symbol.for(name); -} - -// A wrapper around a promise to prevent `Promise>` collapsing into -// `Promise`. -class PromiseLayer { - constructor(promise) { - this.promise = promise; - } - - static wrap(value) { - return value instanceof Promise ? new PromiseLayer(value) : value; - } - - static unwrap(value) { - return value instanceof PromiseLayer ? value.promise : value; - } -} - -export function newPromise(executor) { - return new Promise((resolve) => - executor((value) => { - resolve(PromiseLayer.wrap(value)); - }) - ); -} - -export function resolve(value) { - return Promise.resolve(PromiseLayer.wrap(value)); -} - -export function then(promise, fn) { - return promise.then((value) => fn(PromiseLayer.unwrap(value))); -} - -export function map_promise(promise, fn) { - return promise.then((value) => - PromiseLayer.wrap(fn(PromiseLayer.unwrap(value))) - ); -} - -export function rescue(promise, fn) { - return promise.catch((error) => fn(error)); -} - -class Reference { - constructor(value) { - this.value = value; - } -} - -export function dereference(reference) { - return reference.value; -} - -export function make_reference(value) { - return new Reference(value); -} - -export function set_reference(ref, value) { - let previous = ref.value; - ref.value = value; - return previous; -} - -export function reference_equal(a,b) { - return a === b -} - -export function all_promises(...promises) { - if (promises.length === 1) { - return Promise.all(promises[0]); - } else { - return Promise.all(promises); - } -} - -export function race_promises(...promises) { - if (promises.length === 1) { - return Promise.race(promises[0]); - } else { - return Promise.race(promises); - } -} - -export function map_new() { - return new Map(); -} - -export function map_set(map, key, value) { - return map.set(key, value); -} - -export function map_get(map, key) { - if (map.has(key)) { - return new Ok(map.get(key)); - } - return new Error(undefined); -} - -export function map_size(map) { - return map.size; -} diff --git a/src/gleam/javascript.gleam b/src/gleam/javascript.gleam index 9df0828..f75a2fd 100644 --- a/src/gleam/javascript.gleam +++ b/src/gleam/javascript.gleam @@ -33,7 +33,7 @@ pub type Symbol /// For further information view the MDN documentation: /// /// -@external(javascript, "../ffi.mjs", "type_of") +@external(javascript, "../gleam_javascript_ffi.mjs", "type_of") pub fn type_of(a: value) -> TypeOf /// Use the JavaScript `Symbol.for` method to look up a symbol with the given @@ -42,22 +42,22 @@ pub fn type_of(a: value) -> TypeOf /// For further information see the MDN documentation: /// /// -@external(javascript, "../ffi.mjs", "get_symbol") +@external(javascript, "../gleam_javascript_ffi.mjs", "get_symbol") pub fn get_symbol(a: String) -> Symbol @deprecated("The Reference type is being removed from this packge") pub type Reference(value) @deprecated("The Reference type is being removed from this packge") -@external(javascript, "../ffi.mjs", "dereference") +@external(javascript, "../gleam_javascript_ffi.mjs", "dereference") pub fn dereference(a: Reference(a)) -> a @deprecated("The Reference type is being removed from this packge") -@external(javascript, "../ffi.mjs", "set_reference") +@external(javascript, "../gleam_javascript_ffi.mjs", "set_reference") pub fn set_reference(a: Reference(a), b: a) -> a @deprecated("The Reference type is being removed from this packge") -@external(javascript, "../ffi.mjs", "make_reference") +@external(javascript, "../gleam_javascript_ffi.mjs", "make_reference") pub fn make_reference(a: a) -> Reference(a) @deprecated("The Reference type is being removed from this packge") @@ -68,5 +68,5 @@ pub fn update_reference(ref: Reference(a), f: fn(a) -> a) -> a { } @deprecated("The Reference type is being removed from this packge") -@external(javascript, "../ffi.mjs", "reference_equal") +@external(javascript, "../gleam_javascript_ffi.mjs", "reference_equal") pub fn reference_equal(a: Reference(a), b: Reference(a)) -> Bool diff --git a/src/gleam/javascript/array.gleam b/src/gleam/javascript/array.gleam index e644236..fc9d879 100644 --- a/src/gleam/javascript/array.gleam +++ b/src/gleam/javascript/array.gleam @@ -15,14 +15,14 @@ pub fn to_list(a: Array(element)) -> List(element) /// /// Runs in linear time. /// -@external(javascript, "../../ffi.mjs", "toArray") +@external(javascript, "../../gleam_javascript_ffi.mjs", "toArray") pub fn from_list(a: List(element)) -> Array(element) /// Get the number of elements in the array. /// /// Runs in constant time. /// -@external(javascript, "../../ffi.mjs", "length") +@external(javascript, "../../gleam_javascript_ffi.mjs", "length") pub fn size(a: Array(element)) -> Int /// Returns a new array containing only the elements of the first array after @@ -37,7 +37,7 @@ pub fn size(a: Array(element)) -> Int /// from_list([4, 8, 12]) /// ``` /// -@external(javascript, "../../ffi.mjs", "map") +@external(javascript, "../../gleam_javascript_ffi.mjs", "map") pub fn map(a: Array(a), with with: fn(a) -> b) -> Array(b) /// Reduces a list of elements into a single value by calling a given function @@ -48,7 +48,7 @@ pub fn map(a: Array(a), with with: fn(a) -> b) -> Array(b) /// /// Runs in linear time. /// -@external(javascript, "../../ffi.mjs", "reduce") +@external(javascript, "../../gleam_javascript_ffi.mjs", "reduce") pub fn fold(over over: Array(e), from from: a, with with: fn(a, e) -> a) -> a /// Reduces a list of elements into a single value by calling a given function @@ -59,7 +59,7 @@ pub fn fold(over over: Array(e), from from: a, with with: fn(a, e) -> a) -> a /// /// Runs in linear time. /// -@external(javascript, "../../ffi.mjs", "reduceRight") +@external(javascript, "../../gleam_javascript_ffi.mjs", "reduceRight") pub fn fold_right( over over: Array(e), from from: a, @@ -80,5 +80,5 @@ pub fn fold_right( /// Error(Nil) /// ``` /// -@external(javascript, "../../ffi.mjs", "index") +@external(javascript, "../../gleam_javascript_ffi.mjs", "index") pub fn get(a: Array(e), b: Int) -> Result(e, Nil) diff --git a/src/gleam/javascript/map.gleam b/src/gleam/javascript/map.gleam index a9dcf2e..6ae8f69 100644 --- a/src/gleam/javascript/map.gleam +++ b/src/gleam/javascript/map.gleam @@ -10,7 +10,7 @@ pub type Map(key, value) /// Create a new `Map` with no contained values. /// -@external(javascript, "../../ffi.mjs", "map_new") +@external(javascript, "../../gleam_javascript_ffi.mjs", "map_new") pub fn new() -> Map(key, value) /// Insert a new key and value into the `Map`. @@ -18,15 +18,15 @@ pub fn new() -> Map(key, value) /// **NOTE:** This function will mutate the `Map` rather than immutably /// updating it. /// -@external(javascript, "../../ffi.mjs", "map_set") +@external(javascript, "../../gleam_javascript_ffi.mjs", "map_set") pub fn set(a: Map(key, value), b: key, c: value) -> Map(key, value) /// Get the value for a given key in the `Map`. /// -@external(javascript, "../../ffi.mjs", "map_get") +@external(javascript, "../../gleam_javascript_ffi.mjs", "map_get") pub fn get(a: Map(key, value), b: key) -> Result(value, Nil) /// Get the number of key-value pairs in the `Map`. /// -@external(javascript, "../../ffi.mjs", "map_size") +@external(javascript, "../../gleam_javascript_ffi.mjs", "map_size") pub fn size(a: Map(key, value)) -> Int diff --git a/src/gleam/javascript/promise.gleam b/src/gleam/javascript/promise.gleam index 86bd983..1fa63f3 100644 --- a/src/gleam/javascript/promise.gleam +++ b/src/gleam/javascript/promise.gleam @@ -28,12 +28,12 @@ pub type Promise(value) /// This function is useful for converting code that uses callbacks into code /// that uses promises. /// -@external(javascript, "../../ffi.mjs", "newPromise") +@external(javascript, "../../gleam_javascript_ffi.mjs", "newPromise") pub fn new(a: fn(fn(value) -> Nil) -> Nil) -> Promise(value) /// Create a promise that resolves immediately. /// -@external(javascript, "../../ffi.mjs", "resolve") +@external(javascript, "../../gleam_javascript_ffi.mjs", "resolve") pub fn resolve(a: value) -> Promise(value) /// If the promise is in an error state then apply a function to convert the @@ -41,7 +41,7 @@ pub fn resolve(a: value) -> Promise(value) /// /// This is the equivilent of the `promise.catch` JavaScript method. /// -@external(javascript, "../../ffi.mjs", "rescue") +@external(javascript, "../../gleam_javascript_ffi.mjs", "rescue") pub fn rescue(a: Promise(value), b: fn(Dynamic) -> value) -> Promise(value) /// Chain a second asynchronous operation onto a promise, so it runs after the @@ -49,13 +49,13 @@ pub fn rescue(a: Promise(value), b: fn(Dynamic) -> value) -> Promise(value) /// /// This is the equivilent of the `promise.then` JavaScript method. /// -@external(javascript, "../../ffi.mjs", "then") +@external(javascript, "../../gleam_javascript_ffi.mjs", "then") pub fn await(a: Promise(a), b: fn(a) -> Promise(b)) -> Promise(b) /// Run a function on the value a promise resolves to, after it has resolved. /// The value returned becomes the new value contained by the promise. /// -@external(javascript, "../../ffi.mjs", "map_promise") +@external(javascript, "../../gleam_javascript_ffi.mjs", "map_promise") pub fn map(a: Promise(a), b: fn(a) -> b) -> Promise(b) /// Run a function on the value a promise resolves to, after it has resolved. @@ -116,7 +116,7 @@ pub fn try_await( /// /// This is the equivilent of the `Promise.all` JavaScript static method. /// -@external(javascript, "../../ffi.mjs", "all_promises") +@external(javascript, "../../gleam_javascript_ffi.mjs", "all_promises") pub fn await2(a: Promise(a), b: Promise(b)) -> Promise(#(a, b)) /// Chain an asynchronous operation onto 3 promises, so it runs after the @@ -124,7 +124,7 @@ pub fn await2(a: Promise(a), b: Promise(b)) -> Promise(#(a, b)) /// /// This is the equivilent of the `Promise.all` JavaScript static method. /// -@external(javascript, "../../ffi.mjs", "all_promises") +@external(javascript, "../../gleam_javascript_ffi.mjs", "all_promises") pub fn await3( a: Promise(a), b: Promise(b), @@ -136,7 +136,7 @@ pub fn await3( /// /// This is the equivilent of the `Promise.all` JavaScript static method. /// -@external(javascript, "../../ffi.mjs", "all_promises") +@external(javascript, "../../gleam_javascript_ffi.mjs", "all_promises") pub fn await4( a: Promise(a), b: Promise(b), @@ -149,7 +149,7 @@ pub fn await4( /// /// This is the equivilent of the `Promise.all` JavaScript static method. /// -@external(javascript, "../../ffi.mjs", "all_promises") +@external(javascript, "../../gleam_javascript_ffi.mjs", "all_promises") pub fn await5( a: Promise(a), b: Promise(b), @@ -163,7 +163,7 @@ pub fn await5( /// /// This is the equivilent of the `Promise.all` JavaScript static method. /// -@external(javascript, "../../ffi.mjs", "all_promises") +@external(javascript, "../../gleam_javascript_ffi.mjs", "all_promises") pub fn await6( a: Promise(a), b: Promise(b), @@ -178,7 +178,7 @@ pub fn await6( /// /// This is the equivilent of the `Promise.all` JavaScript static method. /// -@external(javascript, "../../ffi.mjs", "all_promises") +@external(javascript, "../../gleam_javascript_ffi.mjs", "all_promises") pub fn await_array(a: Array(Promise(a))) -> Promise(Array(a)) /// Chain an asynchronous operation onto an list of promises, so it runs after the @@ -192,16 +192,16 @@ pub fn await_list(xs: List(Promise(a))) -> Promise(List(a)) { |> map(array.to_list) } -@external(javascript, "../../ffi.mjs", "all_promises") +@external(javascript, "../../gleam_javascript_ffi.mjs", "all_promises") fn do_await_list(a: List(Promise(a))) -> Promise(Array(a)) -@external(javascript, "../../ffi.mjs", "race_promises") +@external(javascript, "../../gleam_javascript_ffi.mjs", "race_promises") pub fn race2(a: Promise(a), b: Promise(a)) -> Promise(a) -@external(javascript, "../../ffi.mjs", "race_promises") +@external(javascript, "../../gleam_javascript_ffi.mjs", "race_promises") pub fn race3(a: Promise(a), b: Promise(a), c: Promise(a)) -> Promise(a) -@external(javascript, "../../ffi.mjs", "race_promises") +@external(javascript, "../../gleam_javascript_ffi.mjs", "race_promises") pub fn race4( a: Promise(a), b: Promise(a), @@ -209,7 +209,7 @@ pub fn race4( d: Promise(a), ) -> Promise(a) -@external(javascript, "../../ffi.mjs", "race_promises") +@external(javascript, "../../gleam_javascript_ffi.mjs", "race_promises") pub fn race5( a: Promise(a), b: Promise(a), @@ -218,7 +218,7 @@ pub fn race5( e: Promise(a), ) -> Promise(a) -@external(javascript, "../../ffi.mjs", "race_promises") +@external(javascript, "../../gleam_javascript_ffi.mjs", "race_promises") pub fn race6( a: Promise(a), b: Promise(a), @@ -228,8 +228,8 @@ pub fn race6( f: Promise(a), ) -> Promise(a) -@external(javascript, "../../ffi.mjs", "race_promises") +@external(javascript, "../../gleam_javascript_ffi.mjs", "race_promises") pub fn race_list(a: List(Promise(a))) -> Promise(a) -@external(javascript, "../../ffi.mjs", "race_promises") +@external(javascript, "../../gleam_javascript_ffi.mjs", "race_promises") pub fn race_array(a: Array(Promise(a))) -> Promise(a) diff --git a/src/gleam_javascript_ffi.mjs b/src/gleam_javascript_ffi.mjs new file mode 100644 index 0000000..7c14c10 --- /dev/null +++ b/src/gleam_javascript_ffi.mjs @@ -0,0 +1,167 @@ +import { Ok, Error } from "./gleam.mjs"; +import { + UndefinedType, + ObjectType, + BooleanType, + NumberType, + BigIntType, + StringType, + SymbolType, + FunctionType, +} from "./gleam/javascript.mjs"; + +export function toArray(list) { + return list.toArray(); +} + +export function map(thing, fn) { + return thing.map(fn); +} + +export function length(thing) { + return thing.length; +} + +export function reduce(thing, acc, fn) { + return thing.reduce(fn, acc); +} + +export function reduceRight(thing, acc, fn) { + return thing.reduceRight(fn, acc); +} + +export function index(thing, index) { + return index in thing ? new Ok(thing[index]) : new Error(undefined); +} + +export function object_from_entries(entries) { + return Object.fromEntries(entries); +} + +export function type_of(value) { + switch (typeof value) { + case "undefined": + return new UndefinedType(); + case "object": + return new ObjectType(); + case "boolean": + return new BooleanType(); + case "number": + return new NumberType(); + case "bigint": + return new BigIntType(); + case "string": + return new StringType(); + case "symbol": + return new SymbolType(); + case "function": + return new FunctionType(); + default: + throw new globalThis.Error(`Unexpected typeof ${typeof value}`); + } +} + +export function get_symbol(name) { + return Symbol.for(name); +} + +// A wrapper around a promise to prevent `Promise>` collapsing into +// `Promise`. +class PromiseLayer { + constructor(promise) { + this.promise = promise; + } + + static wrap(value) { + return value instanceof Promise ? new PromiseLayer(value) : value; + } + + static unwrap(value) { + return value instanceof PromiseLayer ? value.promise : value; + } +} + +export function newPromise(executor) { + return new Promise((resolve) => + executor((value) => { + resolve(PromiseLayer.wrap(value)); + }) + ); +} + +export function resolve(value) { + return Promise.resolve(PromiseLayer.wrap(value)); +} + +export function then(promise, fn) { + return promise.then((value) => fn(PromiseLayer.unwrap(value))); +} + +export function map_promise(promise, fn) { + return promise.then((value) => + PromiseLayer.wrap(fn(PromiseLayer.unwrap(value))) + ); +} + +export function rescue(promise, fn) { + return promise.catch((error) => fn(error)); +} + +class Reference { + constructor(value) { + this.value = value; + } +} + +export function dereference(reference) { + return reference.value; +} + +export function make_reference(value) { + return new Reference(value); +} + +export function set_reference(ref, value) { + let previous = ref.value; + ref.value = value; + return previous; +} + +export function reference_equal(a,b) { + return a === b +} + +export function all_promises(...promises) { + if (promises.length === 1) { + return Promise.all(promises[0]); + } else { + return Promise.all(promises); + } +} + +export function race_promises(...promises) { + if (promises.length === 1) { + return Promise.race(promises[0]); + } else { + return Promise.race(promises); + } +} + +export function map_new() { + return new Map(); +} + +export function map_set(map, key, value) { + return map.set(key, value); +} + +export function map_get(map, key) { + if (map.has(key)) { + return new Ok(map.get(key)); + } + return new Error(undefined); +} + +export function map_size(map) { + return map.size; +} -- cgit v1.2.3