Docs for stdlib
This commit is contained in:
+150
-36
@@ -13,22 +13,60 @@ export const jsApi = new LuaTable({
|
||||
* @param args - The arguments to pass to the constructor.
|
||||
* @returns The new instance.
|
||||
*/
|
||||
new: new LuaBuiltinFunction((sf, constructorFn: any, ...args) => {
|
||||
return new constructorFn(...args.map((v) => luaValueToJS(v, sf)));
|
||||
}),
|
||||
new: new LuaBuiltinFunction(
|
||||
(sf, constructorFn: any, ...args) => {
|
||||
return new constructorFn(...args.map((v) => luaValueToJS(v, sf)));
|
||||
},
|
||||
{
|
||||
kind: "builtin",
|
||||
description: "Creates an instance of a JavaScript class.",
|
||||
signatures: ["js.new(constructor, ...): userdata"],
|
||||
parameters: [
|
||||
{
|
||||
name: "constructor",
|
||||
type: "userdata",
|
||||
description: "JavaScript constructor function.",
|
||||
},
|
||||
{
|
||||
name: "...",
|
||||
description: "Constructor arguments converted to JavaScript values.",
|
||||
},
|
||||
],
|
||||
returns: [{ type: "userdata", description: "New JavaScript instance." }],
|
||||
examples: [
|
||||
{ code: 'local value = js.new(js.window.Date, "2024-03-14")' },
|
||||
],
|
||||
},
|
||||
),
|
||||
/**
|
||||
* Imports a JavaScript module.
|
||||
* @param url - The URL of the module to import.
|
||||
* @returns The imported module.
|
||||
*/
|
||||
import: new LuaBuiltinFunction(async (_sf, url) => {
|
||||
let m = await import(url);
|
||||
// Unwrap default if it exists
|
||||
if (Object.keys(m).length === 1 && m.default) {
|
||||
m = m.default;
|
||||
}
|
||||
return m;
|
||||
}),
|
||||
import: new LuaBuiltinFunction(
|
||||
async (_sf, url) => {
|
||||
let m = await import(url);
|
||||
// Unwrap default if it exists
|
||||
if (Object.keys(m).length === 1 && m.default) {
|
||||
m = m.default;
|
||||
}
|
||||
return m;
|
||||
},
|
||||
{
|
||||
kind: "builtin",
|
||||
description: "Dynamically imports a JavaScript module from a URL.",
|
||||
parameters: [{ name: "url", type: "string", description: "Module URL." }],
|
||||
returns: [
|
||||
{
|
||||
type: "userdata",
|
||||
description: "Imported module, with a sole default export unwrapped.",
|
||||
},
|
||||
],
|
||||
examples: [
|
||||
{ code: 'local lib = js.import("https://esm.sh/lodash@4.17.21")' },
|
||||
],
|
||||
},
|
||||
),
|
||||
/**
|
||||
* Like `js.import`, but takes a path to a file in the current space (e.g.
|
||||
* "Library/foo/bar.js") and resolves it to its full same-origin `/.fs` URL
|
||||
@@ -36,51 +74,127 @@ export const jsApi = new LuaTable({
|
||||
* @param path - Space-relative path to the JS module (leading "/" optional).
|
||||
* @returns The imported module (with a sole `default` export unwrapped).
|
||||
*/
|
||||
importFromSpace: new LuaBuiltinFunction(async (_sf, path: string) => {
|
||||
const base = document.baseURI.replace(/\/*$/, "/");
|
||||
const rel = String(path).replace(/^\/+/, "");
|
||||
let m = await import(base + fsEndpoint.slice(1) + "/" + rel);
|
||||
// Unwrap default if it exists (same contract as `js.import`).
|
||||
if (Object.keys(m).length === 1 && m.default) {
|
||||
m = m.default;
|
||||
}
|
||||
return m;
|
||||
}),
|
||||
eachIterable: new LuaBuiltinFunction((_sf, val) => {
|
||||
const iterator = val[Symbol.asyncIterator]();
|
||||
return async () => {
|
||||
const result = await iterator.next();
|
||||
if (result.done) {
|
||||
return;
|
||||
importFromSpace: new LuaBuiltinFunction(
|
||||
async (_sf, path: string) => {
|
||||
const base = document.baseURI.replace(/\/*$/, "/");
|
||||
const rel = String(path).replace(/^\/+/, "");
|
||||
let m = await import(base + fsEndpoint.slice(1) + "/" + rel);
|
||||
// Unwrap default if it exists (same contract as `js.import`).
|
||||
if (Object.keys(m).length === 1 && m.default) {
|
||||
m = m.default;
|
||||
}
|
||||
return result.value;
|
||||
};
|
||||
}),
|
||||
return m;
|
||||
},
|
||||
{
|
||||
kind: "builtin",
|
||||
description:
|
||||
"Imports a JavaScript module from a file in the current space.",
|
||||
parameters: [
|
||||
{
|
||||
name: "path",
|
||||
type: "string",
|
||||
description:
|
||||
"Space-relative module path, with an optional leading slash.",
|
||||
},
|
||||
],
|
||||
returns: [
|
||||
{
|
||||
type: "userdata",
|
||||
description: "Imported module, with a sole default export unwrapped.",
|
||||
},
|
||||
],
|
||||
examples: [
|
||||
{ code: 'local acme = js.importFromSpace("Library/acme/acme.js")' },
|
||||
],
|
||||
},
|
||||
),
|
||||
eachIterable: new LuaBuiltinFunction(
|
||||
(_sf, val) => {
|
||||
const iterator = val[Symbol.asyncIterator]();
|
||||
return async () => {
|
||||
const result = await iterator.next();
|
||||
if (result.done) {
|
||||
return;
|
||||
}
|
||||
return result.value;
|
||||
};
|
||||
},
|
||||
{
|
||||
kind: "builtin",
|
||||
description: "Creates a Lua iterator over a JavaScript async iterable.",
|
||||
parameters: [
|
||||
{
|
||||
name: "iterable",
|
||||
type: "userdata",
|
||||
description: "JavaScript async iterable.",
|
||||
},
|
||||
],
|
||||
returns: [
|
||||
{
|
||||
type: "function",
|
||||
description: "Iterator yielding successive JavaScript values.",
|
||||
},
|
||||
],
|
||||
examples: [
|
||||
{
|
||||
code: "for value in js.eachIterable(someJsAsyncIterable) do\n print(value)\nend",
|
||||
},
|
||||
],
|
||||
},
|
||||
),
|
||||
/**
|
||||
* Converts a JavaScript value to a Lua value.
|
||||
* @param val - The JavaScript value to convert.
|
||||
* @returns The Lua value.
|
||||
*/
|
||||
tolua: new LuaBuiltinFunction((_sf, val) => jsToLuaValue(val)),
|
||||
tolua: new LuaBuiltinFunction((_sf, val) => jsToLuaValue(val), {
|
||||
kind: "builtin",
|
||||
description: "Converts a JavaScript value to its Lua representation.",
|
||||
parameters: [
|
||||
{ name: "value", description: "JavaScript value to convert." },
|
||||
],
|
||||
returns: [{ description: "Converted Lua value." }],
|
||||
examples: [{ code: "local luaTable = js.tolua(jsArray)" }],
|
||||
}),
|
||||
/**
|
||||
* Converts a Lua value to a JavaScript value.
|
||||
* @param val - The Lua value to convert.
|
||||
* @returns The JavaScript value.
|
||||
*/
|
||||
tojs: new LuaBuiltinFunction((sf, val) => luaValueToJS(val, sf)),
|
||||
tojs: new LuaBuiltinFunction((sf, val) => luaValueToJS(val, sf), {
|
||||
kind: "builtin",
|
||||
description: "Converts a Lua value to its JavaScript representation.",
|
||||
parameters: [{ name: "value", description: "Lua value to convert." }],
|
||||
returns: [{ description: "Converted JavaScript value." }],
|
||||
examples: [{ code: "local jsArray = js.tojs({1, 2, 3})" }],
|
||||
}),
|
||||
/**
|
||||
* Logs a message to the console.
|
||||
* @param args - The arguments to log.
|
||||
*/
|
||||
log: new LuaBuiltinFunction((_sf, ...args) => {
|
||||
console.log(...args);
|
||||
}),
|
||||
log: new LuaBuiltinFunction(
|
||||
(_sf, ...args) => {
|
||||
console.log(...args);
|
||||
},
|
||||
{
|
||||
kind: "builtin",
|
||||
description: "Logs values to the JavaScript console.",
|
||||
parameters: [{ name: "...", description: "Values to log." }],
|
||||
examples: [{ code: 'js.log("User data:", {name = "Ada"})' }],
|
||||
},
|
||||
),
|
||||
/**
|
||||
* Converts a Lua value to a JSON string.
|
||||
* @param val - The Lua value to convert.
|
||||
* @returns The JSON string.
|
||||
*/
|
||||
stringify: new LuaBuiltinFunction((_sf, val) => JSON.stringify(val)),
|
||||
stringify: new LuaBuiltinFunction((_sf, val) => JSON.stringify(val), {
|
||||
kind: "builtin",
|
||||
description: "Serializes a value as JSON using JavaScript semantics.",
|
||||
parameters: [{ name: "value", description: "Value to serialize." }],
|
||||
returns: [{ type: "string", description: "JSON representation." }],
|
||||
examples: [{ code: "print(js.stringify({1, 2, 3})) -- [1,2,3]" }],
|
||||
}),
|
||||
|
||||
// Expose the global window object
|
||||
window: globalThis,
|
||||
|
||||
Reference in New Issue
Block a user