browser.js
JavaScript’s strings: JSString, text as JavaScript has it, UTF-16,
beside a String, which is bytes (UTF-8 as a literal writes it, but
any byte may be in one). A string is made one by toJS, and one
made a string by fromJS; there is nothing else to do with one.
It is for where a program meets JavaScript as it is:
- a field of an effect’s operation of type
JSStringis given to its provider, and taken from it, as a JavaScript string, through no helper (aStringfield is bytes, which a provider gives to the host by$toJsor$toBytes); a provider handing much text from one host API to another (what is in a<textarea>, to another element) keeps it aJSString, made aStringonly where the program reads it. The library’s own effects (browser.dom,std.io, …) take and giveStrings, their providers turning them into what the host has; - an ES module (
--backend=esm) gives and takes what it is given as it is: a function for JavaScript to call takes and givesJSStrings,greet : JSString -> JSString, and is aString’s inside, byfromJSandtoJS(aStringit gives is bytes, each a code unit, which JavaScript must decode).
In JavaScript (esm, page) a JavaScript string; in the
interpreter a string of UTF-8, valid, which is all a toJS makes —
so fromJS (toJS s) is R.toValid s on each. A C
program does not import it (E0351), as it does none of the browser’s
modules.
JSString
data JSStringText as JavaScript has it, a JavaScript string: made only by toJS
(no value is written of it), and read only by fromJS.
strToJS
strToJS : String -> JSStringstrFromJS
strFromJS : JSString -> StringtoJS
toJS : String -> JSStringA string’s text as JavaScript’s: its UTF-8 read, what is not UTF-8
in it U+FFFD (each longest start of a rune’s bytes one, as
TextDecoder reads them, a byte order mark kept).
fromJS
fromJS : JSString -> StringJavaScript’s text as a string, UTF-8: a surrogate not in a pair
U+FFFD (as toWellFormed makes it). Back from toJS, a string
is what it was where it was UTF-8.
>>> List.map (\s -> fromJS (toJS s)) ["", "héllo 😀", "a\xffb", "\xed\xa0\x80"]["", "héllo 😀", "a\u{FFFD}b", "\u{FFFD}\u{FFFD}\u{FFFD}"]