Standard library · Web and services

node

A Salam program under Node.js: modules, files, processes and JavaScript objects.

import node
On this page
  1. Functions

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): i64
Loads a Node module with require.
node-Basics.salam
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())
end

Exports

func Exports(): i64
The module.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): i64
A property of an object that is itself an object.

The example for this item is shown with the ones above: node-Basics.salam

GetNum

func GetNum(obj: i64, name: str): f64
A numeric property.

The example for this item is shown with the ones above: node-Basics.salam

GetStr

func GetStr(obj: i64, name: str): str
A string property.

The example for this item is shown with the ones above: node-Basics.salam

GetBool

func GetBool(obj: i64, name: str): bool
A boolean property.

The example for this item is shown with the ones above: node-Basics.salam

SetProp

func SetProp(obj: i64, name: str, value: i64)
Sets a property on an object.

The example for this item is shown with the ones above: node-Basics.salam

SetStr

func SetStr(obj: i64, name: str, value: str)
A string property.

The example for this item is shown with the ones above: node-Basics.salam

SetNum

func SetNum(obj: i64, name: str, value: f64)
A numeric property.

The example for this item is shown with the ones above: node-Basics.salam

SetBool

func SetBool(obj: i64, name: str, value: bool)
A boolean property.

The example for this item is shown with the ones above: node-Basics.salam

SetPropFn0

func SetPropFn0(obj: i64, name: str, fn: func())
Makes a function of no arguments a property of the object, so JavaScript can call it.

The example for this item is shown with the ones above: node-Basics.salam

SetPropFn1

func SetPropFn1(obj: i64, name: str, fn: func(i64))
Makes a function of one argument a property.

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)
Makes a function of one argument with a return value a property.

The example for this item is shown with the ones above: node-Basics.salam

Call0

func Call0(obj: i64, method: str): i64
Calls a method with no arguments.

The example for this item is shown with the ones above: node-Basics.salam

CallS1

func CallS1(obj: i64, method: str, a: str): i64
With one string.

The example for this item is shown with the ones above: node-Basics.salam

CallS2

func CallS2(obj: i64, method: str, a: str, b: str): i64
With two strings.

The 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): i64
With three strings.

The example for this item is shown with the ones above: node-Basics.salam

CallV1

func CallV1(obj: i64, method: str, a: i64): i64
With one JavaScript value.
node-Calls.salam
import 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)
end

CallV2

func CallV2(obj: i64, method: str, a: i64, b: i64): i64
With two values.

The 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): i64
With three values.

The example for this item is shown with the ones above: node-Calls.salam

CallVS1

func CallVS1(obj: i64, method: str, a: i64, b: str): i64
With one value and one string.

The example for this item is shown with the ones above: node-Calls.salam

CallSB1

func CallSB1(obj: i64, method: str, a: str, b: bool): i64
With one string and one boolean.

The example for this item is shown with the ones above: node-Calls.salam

CallB1

func CallB1(obj: i64, method: str, a: bool): i64
With one boolean.

The example for this item is shown with the ones above: node-Calls.salam

CallFn0

func CallFn0(obj: i64, method: str, fn: func()): i64
With a function of no arguments, as nextTick 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)): i64
With a function of one argument, as forEach 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()): i64
With one string and a function, as on("event", ...) wants.

The example for this item is shown with the ones above: node-Calls.salam

New0

func New0(ctor: i64): i64
new 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): i64
new 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): i64
new with four arguments.

The example for this item is shown with the ones above: node-Calls.salam

Invoke0

func Invoke0(fn: i64): i64
Calls a JavaScript function value.

The example for this item is shown with the ones above: node-Basics.salam

Invoke1

func Invoke1(fn: i64, a: i64): i64
With one argument.

The example for this item is shown with the ones above: node-Basics.salam

Invoke2

func Invoke2(fn: i64, a: i64, b: i64): i64
With two arguments.

The example for this item is shown with the ones above: node-Calls.salam

IsNullish

func IsNullish(x: i64): bool
The value is null or undefined.

The example for this item is shown with the ones above: node-Basics.salam

Global

func Global(name: str): i64
The global object with this name, such as JSON.

The example for this item is shown with the ones above: node-Basics.salam

NewArray

func NewArray(): i64
An empty array.

The example for this item is shown with the ones above: node-Basics.salam

NewObject

func NewObject(): i64
An empty object.

The example for this item is shown with the ones above: node-Basics.salam

Push

func Push(arr: i64, value: i64)
Adds a value to an array.

The example for this item is shown with the ones above: node-Basics.salam

PushStr

func PushStr(arr: i64, value: str)
Adds a string to an array.

The example for this item is shown with the ones above: node-Basics.salam

NewPromise

func NewPromise(executor: func(i64, i64)): i64
A new promise; the function takes the resolve and reject functions.

The example for this item is shown with the ones above: node-Calls.salam

ToStr

func ToStr(x: i64): str
Turns a value into a string with String.

The example for this item is shown with the ones above: node-Basics.salam

ToBool

func ToBool(x: i64): bool
Turns a value into a boolean.

The example for this item is shown with the ones above: node-Basics.salam

Platform

func Platform(): str
The operating system, such as linux or win32.

The example for this item is shown with the ones above: node-Basics.salam

IsWindows

func IsWindows(): bool
It is running on Windows.

The example for this item is shown with the ones above: node-Basics.salam

TmpDir

func TmpDir(): str
The system temporary directory.

The example for this item is shown with the ones above: node-Basics.salam

PathJoin2

func PathJoin2(a: str, b: str): str
Joins two path pieces.

The example for this item is shown with the ones above: node-Basics.salam

PathJoin3

func PathJoin3(a: str, b: str, c: str): str
Three pieces.

The example for this item is shown with the ones above: node-Basics.salam

PathDirname

func PathDirname(p: str): str
The directory of a path.

The example for this item is shown with the ones above: node-Basics.salam

MkdtempSync

func MkdtempSync(prefix: str): str
Makes a unique temporary directory with a prefix.

The example for this item is shown with the ones above: node-Basics.salam

WriteFileSync

func WriteFileSync(p: str, contents: str)
Writes a file.

The example for this item is shown with the ones above: node-Basics.salam

ReadFileSync

func ReadFileSync(p: str): str
Reads a file.

The example for this item is shown with the ones above: node-Basics.salam

RmSyncRecursiveForce

func RmSyncRecursiveForce(p: str)
Deletes a file or a directory with everything in it.

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))
Runs a program with its arguments in the given directory and, once it finishes, calls the function with "it failed", the output and the error output.

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 same, with the Node error object itself.

The example for this item is shown with the ones above: node-Calls.salam