Files
cell/scripts/core/engine.js

725 lines
20 KiB
JavaScript

(function engine() {
prosperon.DOC = Symbol('+documentation+') // Symbol for documentation references
globalThis.log = new Proxy({}, {
get(target,prop,receiver) {
return function() {}
}
})
var listeners = new Map()
prosperon.on = function(type, callback) {
if (!listeners.has(type)) listeners.set(type, [])
listeners.get(type).push(callback)
return function() {
var arr = listeners.get(type)
if (!arr) return
var idx = arr.indexOf(callback)
if (idx >= 0) arr.splice(idx,1)
}
}
prosperon.dispatch = function(type, data) {
var arr = listeners.get(type)
if (!arr) return
for (var callback of arr) callback(data)
}
// Get hidden modules from prosperon.hidden before stripping it
var hidden = prosperon.hidden
var actor_mod = hidden.actor
var wota = hidden.wota
var console_mod = hidden.console
var use_embed = hidden.use_embed
var use_dyn = hidden.use_dyn
var enet = hidden.enet
var nota = hidden.nota
// Strip hidden from prosperon so nothing else can access it
delete prosperon.hidden
var os = use_embed('os')
var js = use_embed('js')
prosperon.on('SIGINT', function() {
os.exit(1)
})
prosperon.on('SIGABRT', function() {
console.error(new Error('SIGABRT'))
os.exit(1)
})
prosperon.on('SIGSEGV', function() {
console.error(new Error('SIGSEGV'))
os.exit(1)
})
var io = use_embed('io')
globalThis.console = console_mod
var RESPATH = 'scripts/modules/resources.js'
var canonical = io.realdir(RESPATH) + 'resources.js'
var content = io.slurp(RESPATH)
var resources = js.eval(RESPATH, `(function setup_resources(io){${content}})`).call({}, io)
var use_cache = {}
use_cache['resources'] = resources
function print_api(obj) {
for (var prop in obj) {
if (!obj.hasOwnProperty(prop)) continue
var val = obj[prop]
console.log(prop)
if (typeof val === 'function') {
var m = val.toString().match(/\(([^)]*)\)/)
if (m) console.log(' function: ' + prop + '(' + m[1].trim() + ')')
}
}
}
prosperon.PATH = [
"/",
"scripts/modules/"
]
var res_cache = {}
function console_rec(category, priority, line, file, msg) {
return `[${prosperon.id.slice(0,5)}] [${file}:${line}: [${category} ${priority}]: ${msg}\n`
var now = time.now()
var id = prosperon.name ? prosperon.name : prosperon.id
id = id.substring(0,6)
return `[${id}] [${time.text(now, "mb d yyyy h:nn:ss")}] ${file}:${line}: [${category} ${priority}]: ${msg}\n`
}
function pprint(msg, lvl = 0) {
var file = "nofile"
var line = 0
var caller = new Error().stack.split("\n")[2]
if (caller) {
var md = caller.match(/\((.*)\:/)
var m = md ? md[1] : "SCRIPT"
if (m) file = m
md = caller.match(/\:(\d*)\)/)
m = md ? md[1] : 0
if (m) line = m
}
var fmt = console_rec("script", lvl, line, file, msg)
console.print(fmt)
}
function format_args(...args) {
return args.map(arg => {
if (typeof arg === 'object' && arg !== null) {
try {
return json.encode(arg)
} catch (e) {
return String(arg)
}
}
return String(arg)
}).join(' ')
}
console.spam = function spam(...args) {
pprint(format_args(...args), 0)
}
console.debug = function debug(...args) {
pprint(format_args(...args), 1)
}
console.info = function info(...args) {
pprint(format_args(...args), 2)
}
console.warn = function warn(...args) {
pprint(format_args(...args), 3)
}
console.log = function log(...args) {
pprint(format_args(...args), 2)
}
console.error = function error(e) {
if (!e)
e = new Error()
if (e instanceof Error)
pprint(`${e.name} : ${e.message}
${e.stack}`, 4)
else {
var stack = new Error()
pprint(`${e}
${stack.stack}`,4)
}
}
console.panic = function panic(e) {
pprint(e, 5)
os.quit()
}
console.assert = function assert(op, str = `assertion failed [value '${op}']`) {
if (!op) console.panic(str)
}
var BASEPATH = 'scripts/core/base.js'
var script = io.slurp(BASEPATH)
var fnname = "base"
script = `(function ${fnname}() { ${script}; })`
js.eval(BASEPATH, script)()
var inProgress = {}
var loadingStack = []
globalThis.use = function use(file, ...args) {
// Normalize the request - remove .js extension if present
var request_name = file
if (file.endsWith('.js')) {
request_name = file.substring(0, file.length - 3)
}
// Check cache first - both 'transform' and 'transform.js' should return same cached value
for (var cached_key in use_cache) {
if (cached_key === file || cached_key === request_name ||
cached_key === request_name + '.js' ||
(cached_key.endsWith('.js') && cached_key.substring(0, cached_key.length - 3) === request_name)) {
return use_cache[cached_key]
}
}
// Check for circular dependencies
if (loadingStack.includes(file)) {
let cycleIndex = loadingStack.indexOf(file)
let cyclePath = loadingStack.slice(cycleIndex).concat(file)
throw new Error(
`Circular dependency detected while loading "${file}".\n` +
`Module chain: ${loadingStack.join(" -> ")}\n` +
`Cycle specifically: ${cyclePath.join(" -> ")}`
)
}
// Try to find the script file
var path = resources.find_script(request_name)
// Check if there's an embedded module
var embed_mod = use_embed(request_name)
// If no script and no embedded module, error
if (!path && !embed_mod) {
throw new Error(`Module ${file} could not be found`)
}
// If only embedded module exists, return it
if (!path && embed_mod) {
use_cache[file] = embed_mod
use_cache[request_name] = embed_mod
if (file !== request_name) {
use_cache[request_name + '.js'] = embed_mod
}
return embed_mod
}
// If we have a script path, check for circular dependency
if (inProgress[path]) {
throw new Error(`Circular dependency detected while loading "${file}"`)
}
inProgress[path] = true
loadingStack.push(file)
// Load and execute the script
var script = io.slurp(path)
var mod_name = path.name()
// Create context - if embedded module exists, script extends it
var context = {}
if (embed_mod)
context.__proto__ = embed_mod
var mod_script = `(function setup_${mod_name}_module(arg){${script};})`
var fn = js.eval(path, mod_script)
// Call the script - pass embedded module as 'this' if it exists
var ret = fn.call(context, args)
// If script doesn't return anything, check if we have embedded module
if (!ret && embed_mod) {
ret = embed_mod
} else if (!ret) {
throw new Error(`Use must be used with a module, but ${path} doesn't return a value`)
}
loadingStack.pop()
delete inProgress[path]
// Cache under all possible keys
use_cache[path] = ret
use_cache[file] = ret
use_cache[request_name] = ret
if (file !== request_name && !file.endsWith('.js')) {
use_cache[request_name + '.js'] = ret
}
return ret
}
globalThis.json = use('json')
var time = use('time')
var DOCPATH = 'scripts/core/doc.js'
var script = io.slurp(DOCPATH)
var fnname = "doc"
script = `(function ${fnname}() { ${script}; })`
/*
When handling a message, the message appears like this:
{
type: type of message
- contact: used for contact messages
- stop: used to issue stop command
- etc
reply: ID this message will respond to (callback saved on the actor)
replycc: the actor that is waiting for the reply
target: ID of the actor that's supposed to receive the message. Only added to non direct sends (out of portals)
return: reply ID so the replycc actor can know what callback to send the message to
data: the actual content of the message
}
actors look like
{
id: the GUID of this actor
address: the IP this actor can be found at
port: the port of the IP the actor can be found at
}
*/
var util = use('util')
var math = use('math')
var crypto = use('crypto')
var dying = false
var HEADER = Symbol()
function create_actor(__ACTORDATA__ = {id:util.guid()}) {
return { __ACTORDATA__ }
}
var $_ = create_actor()
$_.random = crypto.random
$_.random[prosperon.DOC] = "returns a number between 0 and 1. There is a 50% chance that the result is less than 0.5."
$_.clock = function(fn) { return os.now() }
$_.clock[prosperon.DOC] = "takes a function input value that will eventually be called with the current time in number form."
var underlings = new Set()
var overling = undefined
var root = undefined
// Don't make $_ global - it should only be available to actor scripts
var receive_fn = undefined
var greeters = {}
function is_actor(actor) {
return actor.__ACTORDATA__
}
globalThis.is_actor = is_actor;
function peer_connection(peer) {
return {
latency: peer.rtt,
bandwidth: {
incoming: peer.incoming_bandwidth,
outgoing: peer.outgoing_bandwidth
},
activity: {
last_sent: peer.last_send_time,
last_received: peer.last_receive_time
},
mtu: peer.mtu,
data: {
incoming_total: peer.incoming_data_total,
outgoing_total: peer.outgoing_data_total,
reliable_in_transit: peer.reliable_data_in_transit
},
latency_variance: peer.rtt_variance,
packet_loss: peer.packet_loss,
state: peer.state
}
}
$_.connection = function(callback, actor, config) {
var peer = peers[actor.__ACTORDATA__.id]
if (peer) {
callback(peer_connection(peer))
return
}
if (actor_mod.mailbox_exist(actor.__ACTORDATA__.id)) {
callback({type:"local"})
return
}
throw new Error(`Could not get connection information for ${actor}`)
}
$_.connection[prosperon.DOC] = "The connection function takes a callback function, an actor object, and a configuration record for getting information about the status of a connection to the actor. The configuration record is used to request the sort of information that needs to be communicated. This can include latency, bandwidth, activity, congestion, cost, partitions. The callback is given a record containing the requested information."
var peers = {}
var id_address = {}
var peer_queue = new WeakMap()
var portal = undefined
var portal_fn = undefined
var service_delay = 0.01
$_.portal = function(fn, port) {
if (portal) throw new Error(`Already started a portal listening on ${portal.port}`)
if (!port) throw new Error("Requires a valid port.")
console.log(`starting a portal on port ${port}`)
portal = enet.create_host({address: "any", port})
portal_fn = fn
}
$_.portal[prosperon.DOC] = "A portal is a special actor with a public address that performs introduction services. It listens on a specified port for contacts by external actors that need to acquire an actor object. The function will receive the record containing the request. The record can have a reply sent through it. A portal can respond by beginning a new actor, or finding an existing actor, or by forwarding the contact message to another actor. This is how distributed Misty networks are bootstrapped. The portal function returns null."
function handle_host(e) {
switch (e.type) {
case "connect":
console.log(`connected a new peer: ${e.peer.address}:${e.peer.port}`)
peers[`${e.peer.address}:${e.peer.port}`] = e.peer
var queue = peer_queue.get(e.peer)
if (queue) {
for (var msg of queue) e.peer.send(nota.encode(msg))
console.log(`sent ${json.encode(msg)} out of queue`)
peer_queue.delete(e.peer)
}
break
case "disconnect":
peer_queue.delete(e.peer)
for (var id in peers) if (peers[id] === e.peer) delete peers[id]
console.log('portal got disconnect from ' + e.peer.address + ":" + e.peer.port)
break
case "receive":
var data = nota.decode(e.data)
// console.log(`got message ${json.encode(data)} over the wire`)
if (data.replycc && !data.replycc.address) {
data.replycc.__ACTORDATA__.address = e.peer.address
data.replycc.__ACTORDATA__.port = e.peer.port
}
// Also populate address/port for any actor objects in the message data
function populate_actor_addresses(obj) {
if (typeof obj !== 'object' || obj === null) return
if (obj.__ACTORDATA__ && !obj.__ACTORDATA__.address) {
obj.__ACTORDATA__.address = e.peer.address
obj.__ACTORDATA__.port = e.peer.port
}
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
populate_actor_addresses(obj[key])
}
}
}
if (data.data) populate_actor_addresses(data.data)
// console.log(`turned it into ${json.encode(data)} over the wire`)
handle_message(data)
break
}
}
$_.contact = function(callback, record) {
send(create_actor(record), record, callback)
}
$_.contact[prosperon.DOC] = `The contact function sends a message to a portal on another machine to obtain an actor object.
The callback is a function with a actor input and a reason input. If successful, actor is bound to an actor object. If not successful, actor is null and reason may contain an explanation.`
$_.receiver = function receiver(fn) {
receive_fn = fn
}
$_.receiver[prosperon.DOC] = "registers a function that will receive all messages..."
$_.start = function start(cb, prg, arg) {
if (dying) {
console.warn(`Cannot start an underling in the same turn as we're stopping`)
return
}
var id = util.guid()
greeters[id] = cb
var argv = ["./prosperon", "spawn", "--id", id, "--overling", json.encode($_), "--root", json.encode(root)]
if (prg) argv = argv.concat(['--program', prg])
if (arg) argv = argv.concat(cmd.encode(arg))
underlings.add(id)
actor_mod.createactor(argv)
}
$_.start[prosperon.DOC] = "The start function creates a new actor..."
$_.stop = function stop(actor) {
if (!actor) {
destroyself()
return
}
if (!is_actor(actor))
throw new Error('Can only call stop on an actor.')
if (!underlings.has(actor.__ACTORDATA__.id))
throw new Error('Can only call stop on an underling or self.')
actor_prep(actor, {type:"stop", id: prosperon.id})
}
$_.stop[prosperon.DOC] = "The stop function stops an underling."
$_.unneeded = function unneeded(fn, seconds) {
actor_mod.unneeded(fn, seconds)
}
$_.unneeded[prosperon.DOC] = "registers a function that is called when the actor..."
$_.delay = function delay(fn, seconds) {
function delay_turn() {
fn()
send_messages()
}
var id = actor_mod.delay(delay_turn, seconds)
return function() { actor_mod.removetimer(id) }
}
$_.delay[prosperon.DOC] = "used to schedule the invocation of a function..."
var couplings = new Set()
$_.couple = function couple(actor) {
console.log(`coupled to ${actor.__ACTORDATA__.id}`)
couplings.add(actor.__ACTORDATA__.id)
}
$_.couple[prosperon.DOC] = "causes this actor to stop when another actor stops."
function actor_prep(actor, send) {
message_queue.push({actor,send});
}
function actor_send(actor, message) {
if (actor[HEADER] && !actor[HEADER].replycc) // attempting to respond to a message but sender is not expecting; silently drop
return
if (!is_actor(actor) ) throw new Error(`Must send to an actor object. Attempted send to ${json.encode(actor)}`)
if (typeof message !== 'object') throw new Error('Must send an object record.')
// message to self
if (actor.__ACTORDATA__.id === prosperon.id) {
if (receive_fn) receive_fn(message.data)
return
}
// message to actor in same flock
if (actor.__ACTORDATA__.id && actor_mod.mailbox_exist(actor.__ACTORDATA__.id)) {
actor_mod.mailbox_push(actor.__ACTORDATA__.id, message)
return
}
if (actor.__ACTORDATA__.address) {
if (actor.__ACTORDATA__.id)
message.target = actor.__ACTORDATA__.id
else
message.type = "contact"
var peer = peers[actor.__ACTORDATA__.address + ":" + actor.__ACTORDATA__.port]
if (!peer) {
if (!portal) {
console.log(`creating a contactor ...`)
portal = enet.create_host({address:"any"})
console.log(`allowing contact to port ${portal.port}`)
}
console.log(`no peer! connecting to ${actor.__ACTORDATA__.address}:${actor.__ACTORDATA__.port}`)
peer = portal.connect(actor.__ACTORDATA__.address, actor.__ACTORDATA__.port)
peer_queue.set(peer, [message])
} else {
peer.send(nota.encode(message))
}
return
}
throw new Error(`Unable to send message to actor ${json.encode(actor)}`)
}
// Holds all messages queued during the current turn.
var message_queue = []
function send_messages() {
// Attempt to flush the queued messages. If one fails, keep going anyway.
var errors = []
while (message_queue.length > 0) {
var item = message_queue.shift()
var actor = item.actor
var send = item.send
try {
actor_send(actor, send)
} catch (err) {
errors.push(err)
}
}
if (errors.length > 0) {
console.error("Some messages failed to send:", errors)
for (var i of errors) console.error(i)
}
}
var replies = {}
function _send(actor, message, reply) {
if (typeof message !== 'object')
throw new Error('Message must be an object')
var send = {type:"user", data: message}
if (actor[HEADER] && actor[HEADER].replycc) {
var header = actor[HEADER]
if (!header.replycc || !is_actor(header.replycc))
throw new Error(`Supplied actor had a return, but it's not a valid actor! ${json.encode(actor[HEADER])}`)
actor = header.replycc
send.return = header.reply
}
if (reply) {
var id = util.guid()
replies[id] = reply
send.reply = id
send.replycc = $_ // This still references the engine's internal $_
}
// Instead of sending immediately, queue it
actor_prep(actor,send);
}
Object.defineProperty(globalThis, 'send', {
value: _send,
writable: false,
configurable: false,
enumerable: true
});
var cmd = use('cmd')
cmd.process(prosperon.argv.slice())
if (!prosperon.args.id) prosperon.id = util.guid()
else prosperon.id = prosperon.args.id
$_.__ACTORDATA__.id = prosperon.id
function turn(msg)
{
try {
handle_message(msg)
send_messages()
} catch (err) {
message_queue = []
throw err
}
}
actor_mod.register_actor(prosperon.id, turn, prosperon.args.main)
if (prosperon.args.overling) overling = json.decode(prosperon.args.overling)
if (prosperon.args.root) root = json.decode(prosperon.args.root)
else root = $_
if (overling) actor_prep(overling, {type:'greet', actor: $_})
if (!prosperon.args.program)
os.exit(1)
if (typeof prosperon.args.program !== 'string')
prosperon.args.program = 'main.js';
actor_mod.setname(prosperon.args.program)
function destroyself() {
console.log(`Got the message to destroy self.`)
dying = true
for (var i of underlings)
$_.stop(create_actor({id:i}))
actor_mod.destroy()
}
function handle_actor_disconnect(id) {
var greeter = greeters[id]
if (greeter) {
greeter({type: "stopped", id})
delete greeters[id]
}
console.log(`actor ${id} disconnected`)
if (couplings.has(id)) $_.stop()
delete peers[id]
}
function handle_message(msg) {
if (msg.target) {
if (msg.target !== prosperon.id) {
actor_mod.mailbox_push(msg.target, msg)
return
}
}
switch (msg.type) {
case "user":
var letter = msg.data
delete msg.data
letter[HEADER] = msg
if (msg.return) {
log.trace(`Received a message for the return id ${msg.return}`)
var fn = replies[msg.return]
if (!fn) throw new Error(`Could not find return function for message ${msg.return}`)
fn(letter)
delete replies[msg.return]
return
}
if (receive_fn) receive_fn(letter)
break
case "stop":
if (msg.id !== overling.__ACTORDATA__.id)
throw new Error(`Got a message from an actor ${msg.id} to stop...`)
destroyself()
break
case "contact":
if (portal_fn) {
var letter2 = msg.data
letter2[HEADER] = msg
delete msg.data
portal_fn(letter2)
} else throw new Error('Got a contact message, but no portal is established.')
break
case "stopped":
handle_actor_disconnect(msg.id)
break
case "greet":
var greeter = greeters[msg.actor.__ACTORDATA__.id]
if (greeter) greeter(msg)
break;
default:
if (receive_fn) receive_fn(msg)
break;
}
};
function enet_check()
{
if (portal) portal.service(handle_host)
$_.delay(enet_check, service_delay);
}
//enet_check();
// Finally, run the program
var prog = io.slurp(prosperon.args.program)
var prog_script = `(function ${prosperon.args.program.name()}($_) { ${prog} })`
var val = js.eval(prosperon.args.program, prog_script)($_)
if (val)
throw new Error('Program must not return anything');
send_messages()
})()