Designing a host standard library
ReScripted does not ship a huge stdlib. The host is the stdlib. You invent the surface and bind it.
Steps
- Write the function names in the language JSON:
builtins.functions:["print", "len", "abs", "spawn"]custom.host_functions: same or extrastandard_library.available:trueif you are providing a setstandard_library.modules: names of packages (["math", "fs"])standard_library.automatically_imported: those names are imported at the start of everyrun(after youadd_librarythem)
- Shipping stdlib: compile
stdlib/math.c++with the app (target_sources). In C++ callengine.add_library("math", rescripted::library::math). Do not have CMake register names. See libraries.md. - Runtime packages:
add_library/add_library_file/load_pluginafter startup. - Put example calls in
exampleson the JSON (hello_world,function, …). - Tell the user in a short comment in their
main.cppwhat each bind does.
Naming
- Script-facing names should look like the language (
print,len), notRescripted_Print. - For large APIs prefer
add_library("math", …)andimport math, or prefixes:fs_read,math_sin,ent_spawn. - If the user
fn print()in a script,runoverwrites the hostprintuntilunload. Mention that.
Defaults you may replace
| Name | Default behavior |
|---|---|
print |
Join args with spaces, return that string (no stdout) |
len |
Length of array, string, or map; else 0 |
str |
to_string of first arg |
type |
"null", "bool", "number", "string", "array", "map", "function" |
Unsafe binds
Do not bind filesystem, eval-of-C++, or network unless the user asked. Scripts are arbitrary text loaded at runtime. See SECURITY.md in the ReScripted repo.
Standard library as scripts
engine.add_library_file("prelude", "stdlib/prelude.mylang") makes import prelude load that file at runtime. Embed the same file and add_library("prelude", source, language) to ship it in the binary. Document both layers in the user’s repo.