From 61fd456ecf2763202df299ab92b09adffe2c2c2d Mon Sep 17 00:00:00 2001 From: Louis Pilfold Date: Sun, 29 Aug 2021 16:15:15 +0100 Subject: JavaScript module --- src/ffi.js | 41 +++++++++++++++++++++++++++++++++++++++++ src/gleam/javascript.gleam | 19 +++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 src/gleam/javascript.gleam (limited to 'src') diff --git a/src/ffi.js b/src/ffi.js index c0b2076..07e7b0f 100644 --- a/src/ffi.js +++ b/src/ffi.js @@ -1,4 +1,14 @@ import { Ok, Error } from "./gleam.js"; +import { + UndefinedType, + ObjectType, + BooleanType, + NumberType, + BigIntType, + StringType, + SymbolType, + FunctionType, +} from "./gleam/javascript.js"; export function toArray(list) { return list.toArray(); @@ -23,3 +33,34 @@ export function reduceRight(thing, acc, fn) { 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); +} diff --git a/src/gleam/javascript.gleam b/src/gleam/javascript.gleam new file mode 100644 index 0000000..590dece --- /dev/null +++ b/src/gleam/javascript.gleam @@ -0,0 +1,19 @@ +// TODO: docs +pub type TypeOf { + UndefinedType + ObjectType + BooleanType + NumberType + BigIntType + StringType + SymbolType + FunctionType +} + +pub external type Symbol + +pub external fn type_of(value) -> TypeOf = + "../ffi.js" "type_of" + +pub external fn get_symbol(String) -> Symbol = + "../ffi.js" "get_symbol" -- cgit v1.2.3