From aa18a8c8d2aaeea87e9427f28d51a0df44aeb3fe Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Thu, 18 Dec 2025 11:20:39 -0600 Subject: [PATCH] remove json as a global --- benchmarks/nbody.ce | 2 +- benchmarks/nota.ce | 1 + benchmarks/wota_nota_json.ce | 2 +- examples/http_download_actor.ce | 2 +- examples/nat.ce | 6 ++-- examples/nat_client.ce | 4 +-- internal/engine.cm | 53 +++++++++++++-------------------- shop.cm | 3 +- tests/contact.cm | 6 ++-- tests/httpget.cm | 2 +- tests/parseq.cm | 2 +- tests/portal_actor.ce | 2 +- tests/pronto.cm | 2 +- tests/reply_actor.ce | 2 +- tests/send.cm | 2 +- tests/toml.cm | 4 +-- 16 files changed, 43 insertions(+), 52 deletions(-) diff --git a/benchmarks/nbody.ce b/benchmarks/nbody.ce index 6a341a0e..310d0b6d 100644 --- a/benchmarks/nbody.ce +++ b/benchmarks/nbody.ce @@ -182,7 +182,7 @@ if (fn_info.locals && fn_info.locals.length > 0) { log.console(` stack_size: ${fn_info.stack_size}`) // Display disassembly -log.console(json.encode(js.disassemble(advance))) +log.console(js.disassemble(advance)) log.console(js.disassemble(advance).length) $_.stop() \ No newline at end of file diff --git a/benchmarks/nota.ce b/benchmarks/nota.ce index 569832db..aba95555 100644 --- a/benchmarks/nota.ce +++ b/benchmarks/nota.ce @@ -1,6 +1,7 @@ var nota = use('nota') var os = use('os') var io = use('fd') +var json = use('json') var ll = io.slurp('benchmarks/nota.json') diff --git a/benchmarks/wota_nota_json.ce b/benchmarks/wota_nota_json.ce index 97954fc7..f445cedf 100644 --- a/benchmarks/wota_nota_json.ce +++ b/benchmarks/wota_nota_json.ce @@ -199,6 +199,6 @@ var result = { decodeNsPerOp: (decodeTime / totalOps) * 1e9 }; -log.console(json.encode(result)); +log.console(result); $_.stop() diff --git a/examples/http_download_actor.ce b/examples/http_download_actor.ce index 0788851c..bd4f5123 100644 --- a/examples/http_download_actor.ce +++ b/examples/http_download_actor.ce @@ -101,7 +101,7 @@ $_.receiver(function(msg) { break; case 'status': - log.console(`got status request. current is ${json.encode(get_status())}`) + log.console(`got status request. current is ${get_status()}`) send(msg, { type: 'status_response', ...get_status() diff --git a/examples/nat.ce b/examples/nat.ce index a082fc32..cdf5f1da 100644 --- a/examples/nat.ce +++ b/examples/nat.ce @@ -10,11 +10,11 @@ var match_id = 0; $_.portal(e => { log.console("NAT server: received connection request"); - if (!is_actor(e.actor)) + if (!isa(e.actor, actor)) send(e, {reason: "Must provide the actor you want to connect."}); if (waiting_client) { - log.console(`sending out messages! to ${json.encode(e.actor)} and ${json.encode(waiting_client.actor)}`) + log.console(`sending out messages! to ${e.actor} and ${waiting_client.actor}`) send(waiting_client, e.actor) send(e, waiting_client.actor) @@ -25,5 +25,5 @@ $_.portal(e => { waiting_client = e - log.console(`actor ${json.encode(e.actor)} is waiting ...`) + log.console(`actor ${e.actor} is waiting ...`) }, 4000); diff --git a/examples/nat_client.ce b/examples/nat_client.ce index 6cf2b9b2..f5a61e03 100644 --- a/examples/nat_client.ce +++ b/examples/nat_client.ce @@ -2,10 +2,10 @@ log.console(`nat client starting`) $_.contact((actor, reason) => { if (actor) { - log.console(`trying to message ${json.encode(actor)}`) + log.console(`trying to message ${actor}`) send(actor, {type:"greet"}) } else { - log.console(json.encode(reason)) + log.console(reason) } }, { address: "108.210.60.32", // NAT server's public IP diff --git a/internal/engine.cm b/internal/engine.cm index 84e52f27..5312d4b9 100644 --- a/internal/engine.cm +++ b/internal/engine.cm @@ -106,6 +106,10 @@ var _ObjectGetPrototypeOf = Object.getPrototypeOf var _ObjectCreate = meme var _ArrayIsArray = Array.isArray +Object.prototype.toString = function() +{ + return json.encode(this) +} function deepFreeze(object) { if (object instanceof blob) @@ -123,6 +127,11 @@ function deepFreeze(object) { return _ObjectFreeze(object); } +globalThis.actor = function() +{ + +} + globalThis.stone = deepFreeze stone.p = function(object) { @@ -194,11 +203,6 @@ globalThis.reverse = function(value) { return null } -var keyCounter = 0 -globalThis.key = function() { - return _Symbol('key_' + (keyCounter++)) -} - globalThis.isa = function(value, master) { if (master == null) return false @@ -221,6 +225,7 @@ globalThis.isa = function(value, master) { if (master == array) return _isArray(value) if (master == object) return typeof value == 'object' && value != null && !_isArray(value) if (master == fn) return typeof value == 'function' + if (master == actor) return isa(value, object) && value[ACTORDATA] // Check prototype chain if (master.prototype) { @@ -388,7 +393,7 @@ os.$_ = $_ var shop = use('shop') globalThis.use = shop.use -globalThis.json = use('json') +var json = use('json') var time = use('time') var pronto = use('pronto') @@ -396,9 +401,6 @@ globalThis.fallback = pronto.fallback globalThis.parallel = pronto.parallel globalThis.race = pronto.race globalThis.sequence = pronto.sequence -globalThis.time_limit = pronto.time_limit -globalThis.requestorize = pronto.requestorize -globalThis.objectify = pronto.objectify var config = { ar_timer: 60, @@ -443,14 +445,9 @@ function guid(bits = 256) return text(guid,'h') } -var HEADER = key() +var _Symbol = Symbol -// returns a number between 0 and 1. There is a 50% chance that the result is less than 0.5. -$_.random = function() { - return (os.random() / 9007199254740992) -} - -$_.random_fit = os.random +var HEADER = _Symbol() // takes a function input value that will eventually be called with the current time in number form. $_.clock = function(fn) { @@ -467,10 +464,6 @@ var root = null var receive_fn = null var greeters = {} // Router functions for when messages are received for a specific actor -globalThis.is_actor = function is_actor(actor) { - return actor[ACTORDATA] -} - function peer_connection(peer) { return { latency: peer.rtt, @@ -532,7 +525,7 @@ function handle_host(e) { var queue = peer_queue.get(e.peer) if (queue) { for (var msg of queue) e.peer.send(nota.encode(msg)) - log.system(`sent ${json.encode(msg)} out of queue`) + log.system(`sent ${msg} out of queue`) peer_queue.delete(e.peer) } break @@ -597,7 +590,7 @@ $_.stop = function stop(actor) { need_stop = true return } - if (!is_actor(actor)) + if (!isa(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.') @@ -653,7 +646,7 @@ 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) && !is_actor(actor.replycc)) throw new Error(`Must send to an actor object. Attempted send to ${json.encode(actor)}`) + if (!isa(actor, actor) && !isa(actor.replycc, actor)) throw new Error(`Must send to an actor object. Attempted send to ${actor}`) if (typeof message != 'object') throw new Error('Must send an object record.') @@ -692,7 +685,7 @@ function actor_send(actor, message) { } return } - log.system(`Unable to send message to actor ${json.encode(actor[ACTORDATA])}`) + log.system(`Unable to send message to actor ${actor[ACTORDATA]}`) } // Holds all messages queued during the current turn. @@ -724,7 +717,7 @@ var replies = {} globalThis.send = function send(actor, message, reply) { if (typeof actor != 'object') - throw new Error(`Must send to an actor object. Provided: ${json.encode(actor)}`); + throw new Error(`Must send to an actor object. Provided: ${actor}`); if (typeof message != 'object') throw new Error('Message must be an object') @@ -732,8 +725,8 @@ globalThis.send = function send(actor, message, reply) { 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])}`) + if (!header.replycc || !isa(header.replycc, actor)) + throw new Error(`Supplied actor had a return, but it's not a valid actor! ${actor[HEADER]}`) actor = header.replycc send.return = header.reply @@ -978,11 +971,7 @@ delete globalThis.unescape delete globalThis.Intl delete globalThis.RegExp -// TODO: delete globalThis - -var mem = js.calc_mem() -log.console(json.encode(mem)) -log.console("total memory usage: " + text(mem.memory_used_size/1024) + " KB"); +_ObjectFreeze(globalThis) $_.clock(_ => { var val = locator.symbol.call(null, $_, cell.args.arg); diff --git a/shop.cm b/shop.cm index 767fe3be..792014b1 100644 --- a/shop.cm +++ b/shop.cm @@ -647,8 +647,9 @@ function execute_module(info) } else { throw new Error(`Module ${info.path} could not be found`) } if (!used) - throw new Error(`Module ${json.encode(info)} returned null`) + throw new Error(`Module ${info} returned null`) + stone(used) return used } diff --git a/tests/contact.cm b/tests/contact.cm index 72fc7eb6..a29c00b7 100644 --- a/tests/contact.cm +++ b/tests/contact.cm @@ -2,14 +2,14 @@ return { test_contact: function() { function contact_fn(actor,reason) { if (actor) { - log.console(`Got an actor: ${json.encode(actor)}`) + log.console(`Got an actor: ${actor}`) send(actor, {greet: "Hello!"}, e => { - log.console(`Got the response ${json.encode(e)}. Goodbye!`); + log.console(`Got the response ${e}. Goodbye!`); }) } else - log.console(`Did not get an actor: ${json.encode(reason)}`) + log.console(`Did not get an actor: ${reason}`) } $_.contact(contact_fn, diff --git a/tests/httpget.cm b/tests/httpget.cm index 1ba6850b..4c1eb82b 100644 --- a/tests/httpget.cm +++ b/tests/httpget.cm @@ -9,7 +9,7 @@ return { $_.start(e => { send(e.actor, { op: 'get', domain: host, port: 80}, addrs => { - log.console(json.encode(addrs[0])) + log.console(addrs[0]) }) }, 'dig') } diff --git a/tests/parseq.cm b/tests/parseq.cm index 0d6e1fb8..27bd1bce 100644 --- a/tests/parseq.cm +++ b/tests/parseq.cm @@ -38,7 +38,7 @@ return { if (reason) log.console(reason) else - log.console(json.encode(result)) + log.console(result) log.console(`took ${time.number()-st} secs`) }); diff --git a/tests/portal_actor.ce b/tests/portal_actor.ce index db4c2b2e..3b3e0900 100644 --- a/tests/portal_actor.ce +++ b/tests/portal_actor.ce @@ -8,7 +8,7 @@ $_.portal(e => { }, 5678); $_.receiver(e => { - log.console(`Got message: ${json.encode(e)}`) + log.console(`Got message: ${e}`) send(e, {greet: "Hello back!"}) $_.delay(_ => $_.stop(), 0.2) }) diff --git a/tests/pronto.cm b/tests/pronto.cm index 4dc9fac6..c161bbc9 100644 --- a/tests/pronto.cm +++ b/tests/pronto.cm @@ -128,7 +128,7 @@ return { req(function(result, reason) { if (result != null) { - log.console(`Objectify result: ${json.encode(result)}`) + log.console(`Objectify result: ${result}`) } else { log.console(`Objectify failed: ${reason}`) } diff --git a/tests/reply_actor.ce b/tests/reply_actor.ce index 6c771962..27b4a48d 100644 --- a/tests/reply_actor.ce +++ b/tests/reply_actor.ce @@ -1,5 +1,5 @@ $_.receiver(e => { - log.console(`Got a message: ${json.encode(e)}`) + log.console(`Got a message: ${e}`) send(e, { message: "Good to go." diff --git a/tests/send.cm b/tests/send.cm index 908da48a..9d7ac9c0 100644 --- a/tests/send.cm +++ b/tests/send.cm @@ -2,7 +2,7 @@ return { test_send: function() { $_.start(e => { send(e.actor, { message: "Hello! Good to go?" }, msg => { - log.console(`Original sender got message back: ${json.encode(msg)}. Stopping!`) + log.console(`Original sender got message back: ${msg}. Stopping!`) // $_.stop() // Removed }) }, "tests/reply_actor") diff --git a/tests/toml.cm b/tests/toml.cm index 002ae077..00ccd22f 100644 --- a/tests/toml.cm +++ b/tests/toml.cm @@ -23,8 +23,8 @@ function test_roundtrip(obj, name) { var encoded = toml.encode(obj) var decoded = toml.decode(encoded) if (!deep_equal(obj, decoded)) { - log.console(name + " - Original:", json.encode(obj)) - log.console(name + " - Round-trip:", json.encode(decoded)) + log.console(name + " - Original:", obj) + log.console(name + " - Round-trip:", decoded) throw name + " round-trip failed" } }