node
A Salam program under Node.js: modules, files, processes and JavaScript objects.
import node
This package is for programs translated to JavaScript with salam js and run under Node.js, the VS Code editor extension for instance. Load any Node or npm module with Require and call its methods with the Call... functions. That is why the examples here are only checked.
JavaScript objects are held as one i64. The call functions are named after the types of their arguments: CallS1 for one string, CallV1 for one JavaScript value.
Functions
Require
func Require(id: str): i64require.import node
func main:
println node.Platform().len() > 0, node.IsWindows() == (node.Platform() == "win32")
folder := node.MkdtempSync(node.PathJoin2(node.TmpDir(), "salam-"))
path := node.PathJoin3(folder, "data", "note.txt")
println node.PathDirname(path) == node.PathJoin2(folder, "data")
fs := node.Require("fs")
_ := node.CallS1(fs, "mkdirSync", node.PathDirname(path))
node.WriteFileSync(path, "hello from node")
println node.ReadFileSync(path)
node.RmSyncRecursiveForce(folder)
obj := node.NewObject()
node.SetStr(obj, "name", "Maryam")
node.SetNum(obj, "age", 30)
node.SetBool(obj, "admin", true)
arr := node.NewArray()
node.PushStr(arr, "a")
node.Push(arr, obj)
node.SetProp(obj, "tags", arr)
println node.GetStr(obj, "name"), node.GetNum(obj, "age"), node.GetBool(obj, "admin")
println node.GetNum(node.GetProp(obj, "tags"), "length")
json := node.Global("JSON")
println node.ToStr(node.CallS1(json, "stringify", "x"))
println node.IsNullish(node.GetProp(obj, "missing")), node.ToBool(node.GetProp(obj, "admin"))
math := node.Global("Math")
println node.ToStr(node.CallV2(math, "max", node.GetProp(obj, "age"), node.GetProp(obj, "age")))
date := node.New1(node.Global("Date"), node.GetProp(obj, "age"))
println node.ToStr(node.Call0(date, "getTime"))
path_module := node.Require("path")
println node.ToStr(node.CallS2(path_module, "join", "a", "b"))
println node.ToStr(node.CallS3(path_module, "join", "a", "b", "c"))
node.SetPropFn0(obj, "hello", ():
println "hello from a function"
end)
_ := node.Call0(obj, "hello")
node.SetPropFn1R(obj, "twice", (x: i64): ret node.CallV2(math, "max", x, x) end)
println node.ToStr(node.Invoke1(node.GetProp(obj, "twice"), node.GetProp(obj, "age")))
println node.IsNullish(node.Exports())
endExports
func Exports(): i64module.exports object; for building a module.The example for this item is shown with the ones above: node-Basics.salam
GetProp
func GetProp(obj: i64, name: str): i64The example for this item is shown with the ones above: node-Basics.salam
GetNum
func GetNum(obj: i64, name: str): f64The example for this item is shown with the ones above: node-Basics.salam
GetStr
func GetStr(obj: i64, name: str): strThe example for this item is shown with the ones above: node-Basics.salam
GetBool
func GetBool(obj: i64, name: str): boolThe example for this item is shown with the ones above: node-Basics.salam
SetProp
func SetProp(obj: i64, name: str, value: i64)The example for this item is shown with the ones above: node-Basics.salam
SetStr
func SetStr(obj: i64, name: str, value: str)The example for this item is shown with the ones above: node-Basics.salam
SetNum
func SetNum(obj: i64, name: str, value: f64)The example for this item is shown with the ones above: node-Basics.salam
SetBool
func SetBool(obj: i64, name: str, value: bool)The example for this item is shown with the ones above: node-Basics.salam
SetPropFn0
func SetPropFn0(obj: i64, name: str, fn: func())The example for this item is shown with the ones above: node-Basics.salam
SetPropFn1
func SetPropFn1(obj: i64, name: str, fn: func(i64))The example for this item is shown with the ones above: node-Basics.salam
SetPropFn1R
func SetPropFn1R(obj: i64, name: str, fn: func(i64): i64)The example for this item is shown with the ones above: node-Basics.salam
Call0
func Call0(obj: i64, method: str): i64The example for this item is shown with the ones above: node-Basics.salam
CallS1
func CallS1(obj: i64, method: str, a: str): i64The example for this item is shown with the ones above: node-Basics.salam
CallS2
func CallS2(obj: i64, method: str, a: str, b: str): i64The example for this item is shown with the ones above: node-Basics.salam
CallS3
func CallS3(obj: i64, method: str, a: str, b: str, c: str): i64The example for this item is shown with the ones above: node-Basics.salam
CallV1
func CallV1(obj: i64, method: str, a: i64): i64import node
func main:
arr := node.New0(node.Global("Array"))
node.PushStr(arr, "b")
node.PushStr(arr, "a")
_ := node.Call0(arr, "sort")
println node.ToStr(node.CallS1(arr, "join", ", "))
number := node.CallV1(node.Global("Math"), "abs", node.GetProp(node.Global("Number"), "MIN_SAFE_INTEGER"))
println node.ToStr(number).len() > 0
println node.ToStr(node.CallV3(node.Global("Math"), "max", number, number, number)) == node.ToStr(number)
date := node.New4(node.Global("Date"), node.GetProp(node.Global("Number"), "EPSILON"), number, number, number)
println node.IsNullish(date)
buf := node.CallSB1(node.NewArray(), "includes", "x", false)
println node.ToStr(buf)
println node.ToStr(node.CallB1(node.Global("JSON"), "stringify", true))
println node.ToStr(node.CallVS1(arr, "fill", node.GetProp(arr, "length"), "x"))
emitter := node.New0(node.GetProp(node.Require("events"), "EventEmitter"))
_ := node.CallSFn0(emitter, "on", "ping", ():
println "the ping event arrived"
end)
_ := node.CallS1(emitter, "emit", "ping")
node.SetPropFn1(emitter, "show", (x: i64):
println "show:", node.ToStr(x)
end)
_ := node.Invoke2(node.GetProp(emitter, "show"), arr, arr)
_ := node.Invoke0(node.GetProp(emitter, "show"))
_ := node.CallFn0(node.Global("process"), "nextTick", ():
println "after the current job"
end)
_ := node.CallFn1(arr, "forEach", (x: i64):
println "element:", node.ToStr(x)
end)
promise := node.NewPromise((resolve: i64, reject: i64):
_ := node.Invoke1(resolve, reject)
end)
println node.IsNullish(promise)
args := node.NewArray()
node.PushStr(args, "-e")
node.PushStr(args, "console.log('hello from the child')")
node.ExecFile("node", args, node.TmpDir(), (failed: bool, out: str, err: str):
println "child:", failed, out.len() > 0, err.len()
end)
node.ExecFileRaw("node", args, node.TmpDir(), (code: i64, out: str, _err: str):
println "raw child:", node.IsNullish(code), out.len() > 0
end)
endCallV2
func CallV2(obj: i64, method: str, a: i64, b: i64): i64The example for this item is shown with the ones above: node-Basics.salam
CallV3
func CallV3(obj: i64, method: str, a: i64, b: i64, c: i64): i64The example for this item is shown with the ones above: node-Calls.salam
CallVS1
func CallVS1(obj: i64, method: str, a: i64, b: str): i64The example for this item is shown with the ones above: node-Calls.salam
CallSB1
func CallSB1(obj: i64, method: str, a: str, b: bool): i64The example for this item is shown with the ones above: node-Calls.salam
CallB1
func CallB1(obj: i64, method: str, a: bool): i64The example for this item is shown with the ones above: node-Calls.salam
CallFn0
func CallFn0(obj: i64, method: str, fn: func()): i64nextTick wants.The example for this item is shown with the ones above: node-Calls.salam
CallFn1
func CallFn1(obj: i64, method: str, fn: func(i64)): i64forEach wants.The example for this item is shown with the ones above: node-Calls.salam
CallSFn0
func CallSFn0(obj: i64, method: str, a: str, fn: func()): i64on("event", ...) wants.The example for this item is shown with the ones above: node-Calls.salam
New0
func New0(ctor: i64): i64new with the given constructor.The example for this item is shown with the ones above: node-Basics.salam
New1
func New1(ctor: i64, a: i64): i64new with one argument.The example for this item is shown with the ones above: node-Basics.salam
New4
func New4(ctor: i64, a: i64, b: i64, c: i64, d: i64): i64new with four arguments.The example for this item is shown with the ones above: node-Calls.salam
Invoke0
func Invoke0(fn: i64): i64The example for this item is shown with the ones above: node-Basics.salam
Invoke1
func Invoke1(fn: i64, a: i64): i64The example for this item is shown with the ones above: node-Basics.salam
Invoke2
func Invoke2(fn: i64, a: i64, b: i64): i64The example for this item is shown with the ones above: node-Calls.salam
IsNullish
func IsNullish(x: i64): boolnull or undefined.The example for this item is shown with the ones above: node-Basics.salam
Global
func Global(name: str): i64JSON.The example for this item is shown with the ones above: node-Basics.salam
NewArray
func NewArray(): i64The example for this item is shown with the ones above: node-Basics.salam
NewObject
func NewObject(): i64The example for this item is shown with the ones above: node-Basics.salam
Push
func Push(arr: i64, value: i64)The example for this item is shown with the ones above: node-Basics.salam
PushStr
func PushStr(arr: i64, value: str)The example for this item is shown with the ones above: node-Basics.salam
NewPromise
func NewPromise(executor: func(i64, i64)): i64The example for this item is shown with the ones above: node-Calls.salam
ToStr
func ToStr(x: i64): strString.The example for this item is shown with the ones above: node-Basics.salam
ToBool
func ToBool(x: i64): boolThe example for this item is shown with the ones above: node-Basics.salam
Platform
func Platform(): strlinux or win32.The example for this item is shown with the ones above: node-Basics.salam
IsWindows
func IsWindows(): boolThe example for this item is shown with the ones above: node-Basics.salam
TmpDir
func TmpDir(): strThe example for this item is shown with the ones above: node-Basics.salam
PathJoin2
func PathJoin2(a: str, b: str): strThe example for this item is shown with the ones above: node-Basics.salam
PathJoin3
func PathJoin3(a: str, b: str, c: str): strThe example for this item is shown with the ones above: node-Basics.salam
PathDirname
func PathDirname(p: str): strThe example for this item is shown with the ones above: node-Basics.salam
MkdtempSync
func MkdtempSync(prefix: str): strThe example for this item is shown with the ones above: node-Basics.salam
WriteFileSync
func WriteFileSync(p: str, contents: str)The example for this item is shown with the ones above: node-Basics.salam
ReadFileSync
func ReadFileSync(p: str): strThe example for this item is shown with the ones above: node-Basics.salam
RmSyncRecursiveForce
func RmSyncRecursiveForce(p: str)The example for this item is shown with the ones above: node-Basics.salam
ExecFile
func ExecFile(cmd: str, args: i64, cwd: str, cb: func(bool, str, str))The example for this item is shown with the ones above: node-Calls.salam
ExecFileRaw
func ExecFileRaw(cmd: str, args: i64, cwd: str, cb: func(i64, str, str))The example for this item is shown with the ones above: node-Calls.salam