Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package.preload["result"] = function()
- -- Generated by the Saturnus compiler 1.0
- -- WARNING! Changes may be discarded at any moment!
- local __saturnus_spread__ = table.unpack or unpack; -- Lua polyfill
- local trait, impl, abstract;
- do
- local __destructure__ = require("std");
- trait = __destructure__.trait;
- impl = __destructure__.impl;
- abstract = __destructure__.abstract;
- end
- local Result = {};
- Result.__meta__ = {};
- Result.__meta__.__call = function(self, struct)
- return setmetatable(struct, self.prototype.__meta__);
- end;
- Result.prototype = {};
- Result.prototype.__proto__ = Result;
- Result.prototype.__meta__ = {};
- Result.prototype.__meta__.__index = Result.prototype;
- setmetatable(Result, Result.__meta__);
- Result.prototype.unwrap = function(self)
- return 0;
- end;
- abstract()(Result.prototype.unwrap, "unwrap", Result, "Result", { is_static = false });
- Result.prototype.unwrap_err = function(self)
- return 0;
- end;
- abstract()(Result.prototype.unwrap_err, "unwrap_err", Result, "Result", { is_static = false });
- Result.prototype.is_err = function(self)
- return 0;
- end;
- abstract()(Result.prototype.is_err, "is_err", Result, "Result", { is_static = false });
- Result.prototype.is_ok = function(self)
- return 0;
- end;
- abstract()(Result.prototype.is_ok, "is_ok", Result, "Result", { is_static = false });
- trait()(Result, "Result");
- local value_symbol = {};
- local Ok = {};
- Ok.__meta__ = {};
- Ok.__meta__.__call = function(self, struct)
- return setmetatable(struct, self.prototype.__meta__);
- end;
- Ok.prototype = {};
- Ok.prototype.__proto__ = Ok;
- Ok.prototype.__meta__ = {};
- Ok.prototype.__meta__.__index = Ok.prototype;
- setmetatable(Ok, Ok.__meta__);
- Ok.new = function(value)
- return Ok({[value_symbol] = value});
- end;
- Ok.prototype.unwrap = function(self)
- return self[value_symbol];
- end;
- Ok.prototype.unwrap_err = function(self)
- return error("Attempting to unwrap error from Ok value");
- end;
- Ok.prototype.is_err = function(self)
- return false;
- end;
- Ok.prototype.is_ok = function(self)
- return true;
- end;
- impl(Result)(Ok, "Ok");
- local Err = {};
- Err.__meta__ = {};
- Err.__meta__.__call = function(self, struct)
- return setmetatable(struct, self.prototype.__meta__);
- end;
- Err.prototype = {};
- Err.prototype.__proto__ = Err;
- Err.prototype.__meta__ = {};
- Err.prototype.__meta__.__index = Err.prototype;
- setmetatable(Err, Err.__meta__);
- Err.new = function(value)
- return Err({[value_symbol] = value});
- end;
- Err.prototype.unwrap = function(self)
- local e = self[value_symbol];
- print(e);
- if type(e) == "table" and type(e.to_string) == "function" then
- error("Unwrap error! " .. tostring(e.to_string(e)));
- elseif e ~= nil then
- error("Unwrap error! " .. tostring(e));
- end
- error("Attempting to unwrap an Err value");
- end;
- Err.prototype.unwrap_err = function(self)
- return self[value_symbol];
- end;
- Err.prototype.is_err = function(self)
- return true;
- end;
- Err.prototype.is_ok = function(self)
- return false;
- end;
- impl(Result)(Err, "Err");
- return {Ok = Ok.new, Err = Err.new, Result = Result};
- end;
- package.preload["tokio"] = function()
- -- Generated by the Saturnus compiler 1.0
- -- WARNING! Changes may be discarded at any moment!
- local __saturnus_spread__ = table.unpack or unpack; -- Lua polyfill
- local Ok, Err;
- do
- local __destructure__ = require("result");
- Ok = __destructure__.Ok;
- Err = __destructure__.Err;
- end
- local yield = coroutine.yield;
- local Promise = {};
- Promise.__meta__ = {};
- Promise.__meta__.__call = function(self, struct)
- return setmetatable(struct, self.prototype.__meta__);
- end;
- Promise.prototype = {};
- Promise.prototype.__proto__ = Promise;
- Promise.prototype.__meta__ = {};
- Promise.prototype.__meta__.__index = Promise.prototype;
- setmetatable(Promise, Promise.__meta__);
- Promise.prototype.resolved = false;
- Promise.prototype.rejected = false;
- Promise.new = function(handler)
- local promise = Promise({});
- local resolve = function(value)
- promise.resolved = true;
- promise.value = value;
- end;
- local reject = function(value)
- promise.rejected = true;
- promise.error = value;
- end;
- handler(resolve, reject);
- return promise;
- end;
- Promise.prototype.on_resolve = function(self, after)
- return Promise.new(function(r)
- local res = await(self);
- r(after(res));
- end);
- end;
- Promise.prototype.await = function(self)
- while true do
- if self.rejected then
- return Err(self.error);
- elseif self.resolved then
- return Ok(self.value);
- end
- yield();
- end
- end;
- local function count(tbl)
- local i = 0;
- for _ in pairs(tbl) do
- i = i + 1;
- end
- return i;
- end
- local Ev = {};
- Ev.__meta__ = {};
- Ev.__meta__.__call = function(self, struct)
- return setmetatable(struct, self.prototype.__meta__);
- end;
- Ev.prototype = {};
- Ev.prototype.__proto__ = Ev;
- Ev.prototype.__meta__ = {};
- Ev.prototype.__meta__.__index = Ev.prototype;
- setmetatable(Ev, Ev.__meta__);
- Ev.prototype.should_exit = false;
- Ev.new = function()
- return Ev({listener_table = {}, coroutines = {}});
- end;
- Ev.prototype.exit = function(self, panic_reason)
- self.panic_reason = panic_reason;
- self.should_exit = true;
- end;
- Ev.prototype.on = function(self, event, handler)
- if self.listener_table[event] == nil then
- self.listener_table[event] = {};
- end
- self.listener_table[event][handler] = true;
- return function()
- return self:off(event, handler);
- end;
- end;
- Ev.prototype.off = function(self, event, handler)
- if self.listener_table[event] == nil then
- return;
- end
- if not self.listener_table[event][handler] then
- return;
- end
- self.listener_table[event][handler] = nil;
- if count(self.listener_table[event]) <= 0 then
- self.listener_table[event] = nil;
- end
- end;
- Ev.prototype.once = function(self, event, handler)
- local wrapper;
- wrapper = function(...)
- local args = {...};
- self:off(event, wrapper);
- handler(__saturnus_spread__(args));
- end;
- return self:on(event, wrapper);
- end;
- Ev.prototype.push = function(self, event, ...)
- local args = {...};
- os.queueEvent(event, __saturnus_spread__(args));
- end;
- Ev.prototype.run = function(self, target)
- if type(target) == "function" then
- local co = coroutine.create(target);
- self.coroutines[co] = co;
- end
- while true do
- if self.should_exit then
- if self.panic_reason ~= nil then
- error(self.panic_reason);
- else
- return nil;
- end
- end
- local event_info = {os.pullEvent()};
- if count(event_info) > 0 then
- self:_fire_listeners(event_info[1], event_info);
- self:_fire_listeners("*", event_info);
- end
- for co in pairs(self.coroutines) do
- if coroutine.status(co) == "dead" then
- self.coroutines[co] = nil;
- else
- local ok, err;
- do
- local __destructure__ = {coroutine.resume(co)};
- ok = __destructure__[1];
- err = __destructure__[2];
- end
- if not ok then
- self:exit(err);
- end
- end
- end
- end
- end;
- Ev.prototype._fire_listeners = function(self, target, info)
- if self.listener_table[target] ~= nil then
- for handler in pairs(self.listener_table[target]) do
- handler(__saturnus_spread__(info));
- end
- end
- end;
- Ev.prototype.launch = function(self, thread)
- local co = coroutine.create(thread);
- self.coroutines[co] = co;
- end;
- Ev.prototype.timeout = function(self, callback, time)
- time = time == nil and 0 or time;
- local clean;
- local timer_id;
- clean = self:on("timer", function(_, id)
- if id == timer_id then
- clean();
- callback();
- end
- end);
- timer_id = os.startTimer(time);
- return clean;
- end;
- Ev.prototype.timeout_promise = function(self, time)
- return Promise.new(function(r)
- return self:timeout(r, time);
- end);
- end;
- local ev = Ev.new();
- local function tokio(alt_ev)
- return function(target)
- alt_ev = alt_ev == nil and ev or alt_ev;
- alt_ev:run(target);
- end;
- end
- return {tokio = tokio, yield = yield, Promise = Promise, Ev = Ev, ev = ev};
- end;
- package.preload["tokio.result"] = function()
- -- Generated by the Saturnus compiler 1.0
- -- WARNING! Changes may be discarded at any moment!
- local __saturnus_spread__ = table.unpack or unpack; -- Lua polyfill
- local trait, impl, abstract;
- do
- local __destructure__ = require("std");
- trait = __destructure__.trait;
- impl = __destructure__.impl;
- abstract = __destructure__.abstract;
- end
- local Result = {};
- Result.__meta__ = {};
- Result.__meta__.__call = function(self, struct)
- return setmetatable(struct, self.prototype.__meta__);
- end;
- Result.prototype = {};
- Result.prototype.__proto__ = Result;
- Result.prototype.__meta__ = {};
- Result.prototype.__meta__.__index = Result.prototype;
- setmetatable(Result, Result.__meta__);
- Result.prototype.unwrap = function(self)
- return 0;
- end;
- abstract()(Result.prototype.unwrap, "unwrap", Result, "Result", { is_static = false });
- Result.prototype.unwrap_err = function(self)
- return 0;
- end;
- abstract()(Result.prototype.unwrap_err, "unwrap_err", Result, "Result", { is_static = false });
- Result.prototype.is_err = function(self)
- return 0;
- end;
- abstract()(Result.prototype.is_err, "is_err", Result, "Result", { is_static = false });
- Result.prototype.is_ok = function(self)
- return 0;
- end;
- abstract()(Result.prototype.is_ok, "is_ok", Result, "Result", { is_static = false });
- trait()(Result, "Result");
- local value_symbol = {};
- local Ok = {};
- Ok.__meta__ = {};
- Ok.__meta__.__call = function(self, struct)
- return setmetatable(struct, self.prototype.__meta__);
- end;
- Ok.prototype = {};
- Ok.prototype.__proto__ = Ok;
- Ok.prototype.__meta__ = {};
- Ok.prototype.__meta__.__index = Ok.prototype;
- setmetatable(Ok, Ok.__meta__);
- Ok.new = function(value)
- return Ok({[value_symbol] = value});
- end;
- Ok.prototype.unwrap = function(self)
- return self[value_symbol];
- end;
- Ok.prototype.unwrap_err = function(self)
- return error("Attempting to unwrap error from Ok value");
- end;
- Ok.prototype.is_err = function(self)
- return false;
- end;
- Ok.prototype.is_ok = function(self)
- return true;
- end;
- impl(Result)(Ok, "Ok");
- local Err = {};
- Err.__meta__ = {};
- Err.__meta__.__call = function(self, struct)
- return setmetatable(struct, self.prototype.__meta__);
- end;
- Err.prototype = {};
- Err.prototype.__proto__ = Err;
- Err.prototype.__meta__ = {};
- Err.prototype.__meta__.__index = Err.prototype;
- setmetatable(Err, Err.__meta__);
- Err.new = function(value)
- return Err({[value_symbol] = value});
- end;
- Err.prototype.unwrap = function(self)
- local e = self[value_symbol];
- print(e);
- if type(e) == "table" and type(e.to_string) == "function" then
- error("Unwrap error! " .. tostring(e.to_string(e)));
- elseif e ~= nil then
- error("Unwrap error! " .. tostring(e));
- end
- error("Attempting to unwrap an Err value");
- end;
- Err.prototype.unwrap_err = function(self)
- return self[value_symbol];
- end;
- Err.prototype.is_err = function(self)
- return true;
- end;
- Err.prototype.is_ok = function(self)
- return false;
- end;
- impl(Result)(Err, "Err");
- return {Ok = Ok.new, Err = Err.new, Result = Result};
- end;
- _G["__saturnus_module_std"] = function()
- -- Generated by the Saturnus compiler 1.0
- -- WARNING! Changes may be discarded at any moment!
- local __saturnus_spread__ = table.unpack or unpack; -- Lua polyfill
- local function panic(message)
- -- <extern "Lua"> --
- error(message);
- -- </extern> --
- end
- local private = {};
- local Vector = {};
- Vector.__meta__ = {};
- Vector.__meta__.__call = function(self, struct)
- return setmetatable(struct, self.prototype.__meta__);
- end;
- Vector.prototype = {};
- Vector.prototype.__proto__ = Vector;
- Vector.prototype.__meta__ = {};
- Vector.prototype.__meta__.__index = Vector.prototype;
- setmetatable(Vector, Vector.__meta__);
- Vector.new = function(data)
- return Vector({[private] = {data = data}});
- end;
- Vector.prototype.at = function(self, key)
- return self[private].data[key + 1];
- end;
- Vector.prototype.set = function(self, key, value)
- self[private].data[key] = value;
- end;
- Vector.prototype.pop = function(self)
- if self.len() > 0 then
- local value = self[private].data[#self[private].data];
- self[private].data[#self[private].data] = nil;
- return value;
- end
- return nil;
- end;
- Vector.prototype.push = function(self, value)
- self[private].data[#self[private].data + 1] = value;
- end;
- Vector.prototype.len = function(self)
- return #self[private].data;
- end;
- Vector.prototype.iter = function(self)
- return Object.entries(self[private].data);
- end;
- local Object = {};
- Object.__meta__ = {};
- Object.__meta__.__call = function(self, struct)
- return setmetatable(struct, self.prototype.__meta__);
- end;
- Object.prototype = {};
- Object.prototype.__proto__ = Object;
- Object.prototype.__meta__ = {};
- Object.prototype.__meta__.__index = Object.prototype;
- setmetatable(Object, Object.__meta__);
- Object.entries = function(tbl)
- -- <extern "Lua"> --
- do
- local iter = pairs(tbl);
- return function(_, next)
- local k, v = iter(tbl, next and next._0);
- if k ~= nil and v ~= nil then
- return { _0 = k, _1 = v }, v;
- end
- end;
- end
- -- </extern> --
- panic("Object.entries() not supported on this platform!");
- end;
- Object.keys = function(tbl)
- -- <extern "Lua"> --
- do
- return pairs(tbl);
- end
- -- </extern> --
- panic("Object.keys() not supported on this platform!");
- end;
- local Tuple = {};
- Tuple.__meta__ = {};
- Tuple.__meta__.__call = function(self, struct)
- return setmetatable(struct, self.prototype.__meta__);
- end;
- Tuple.prototype = {};
- Tuple.prototype.__proto__ = Tuple;
- Tuple.prototype.__meta__ = {};
- Tuple.prototype.__meta__.__index = Tuple.prototype;
- setmetatable(Tuple, Tuple.__meta__);
- Tuple.new = function(data)
- return Vector({[private] = {data = data}});
- end;
- Tuple.prototype.at = function(self, key)
- return self[private].data[key];
- end;
- Tuple.prototype.set = function(self, key, value)
- self[private].data[key] = value;
- end;
- Tuple.prototype.len = function(self)
- return #self[private].data;
- end;
- Tuple.prototype.iter = function(self)
- return Object.entries(self[private].data);
- end;
- local ext = (function()
- local function split(sep)
- return function(self)
- sep = sep == nil and "%s" or sep;
- local t = {};
- for frag in self:gmatch("([^" .. sep .. "]+)") do
- t[#t + 1] = frag;
- end
- return t;
- end;
- end
- local function join(sep)
- return function(self)
- return table.concat(self, sep);
- end;
- end
- local function map(f)
- return function(self)
- local t = {};
- for __destructured_iterator_target__ in Object.entries(self) do
- local k, v;
- do
- local __destructure__ = __destructured_iterator_target__;
- k = __destructure__._0;
- v = __destructure__._1;
- end
- t[k] = f(v, k, self);
- end
- return t;
- end;
- end
- local function reduce(f, seed)
- return function(self)
- for __destructured_iterator_target__ in Object.entries(self) do
- local k, v;
- do
- local __destructure__ = __destructured_iterator_target__;
- k = __destructure__._0;
- v = __destructure__._1;
- end
- seed = f(seed, v, k, self);
- end
- return seed;
- end;
- end
- return {split = split, join = join, map = map, reduce = reduce};
- end)();
- local rtti = {};
- rtti.__meta__ = {};
- rtti.__meta__.__call = function(self, struct)
- return setmetatable(struct, self.prototype.__meta__);
- end;
- rtti.prototype = {};
- rtti.prototype.__proto__ = rtti;
- rtti.prototype.__meta__ = {};
- rtti.prototype.__meta__.__index = rtti.prototype;
- setmetatable(rtti, rtti.__meta__);
- rtti.arguments = function(...)
- local args = {...};
- return function(target, name)
- end;
- end;
- local makeTrait = function(name)
- return function()
- return panic("Trait " .. name .. " is not constructible!");
- end;
- end;
- local function trait()
- return function(target, name)
- target.__meta__.__call = makeTrait(name);
- end;
- end
- local function impl(trait)
- return function(target, name)
- local map = {};
- for __destructured_iterator_target__ in Object.entries(target.prototype) do
- local k, v;
- do
- local __destructure__ = __destructured_iterator_target__;
- k = __destructure__._0;
- v = __destructure__._1;
- end
- map[k] = v;
- end
- for __destructured_iterator_target__ in Object.entries(trait.prototype) do
- local k, v;
- do
- local __destructure__ = __destructured_iterator_target__;
- k = __destructure__._0;
- v = __destructure__._1;
- end
- if map[k] == nil then
- panic(name .. " must implement trait method " .. k .. "!");
- end
- end
- end;
- end
- local function mixin(parent)
- local index = parent.prototype.__meta__.__index;
- if type(index) == "table" then
- index = function(self, key)
- return parent.prototype.__meta__.__index[key];
- end;
- end
- return function(target, name)
- local prev = target.prototype.__meta__.__index;
- if type(prev) == "table" then
- local prev_tbl = prev;
- prev = function(self, key)
- return prev_tbl[key];
- end;
- end
- target.prototype.__meta__.__index = function(self, key)
- return prev(self, key) or index(self, key);
- end;
- end;
- end
- local makePure = function(name)
- return function()
- return panic("Attempting to call abstract method " .. name .. "!");
- end;
- end;
- local function abstract()
- return function(_, name, host, label, meta)
- if meta.is_static then
- host[name] = makePure(label .. "::" .. name);
- else
- host.prototype[name] = makePure(label .. "." .. name);
- end
- end;
- end
- local function __saturnus_operator_pipe_greater(left, right)
- return right(left);
- end
- local function __saturnus_operator_less_pipe(left, right)
- return left(right);
- end
- local function __saturnus_operator_colon_colon(arr, elem)
- -- <extern "Lua"> --
- do
- table.insert(arr, elem);
- return arr;
- end
- -- </extern> --
- panic("Operator :: not supported on this platform");
- end
- local function __saturnus_operator_colon_colon_colon(left, right)
- -- <extern "Lua"> --
- do
- local tbl = {};
- for _, v in pairs(left) do
- tbl[#tbl + 1] = v;
- end
- for _, v in pairs(right) do
- tbl[#tbl + 1] = v;
- end
- return tbl;
- end
- -- </extern> --
- panic("Operator ::: not supported on this platform");
- end
- local function __saturnus_operator_plus_interrogation(left, right)
- return left and (left + right);
- end
- local function __saturnus_operator_plus_interrogation_interrogation(left, right)
- return left and (left + right) or right;
- end
- local function __saturnus_operator_plus_plus_interrogation(left, right)
- return left and (left .. right);
- end
- local function __saturnus_operator_plus_plus_interrogation_interrogation(left, right)
- return left and (left .. right) or right;
- end
- local function __saturnus_operator_minus_interrogation(left, right)
- return left and (left - right);
- end
- local function __saturnus_operator_minus_interrogation_interrogation(left, right)
- return left and (left - right) or right;
- end
- local function __saturnus_operator_times_interrogation(left, right)
- return left and (left * right);
- end
- local function __saturnus_operator_times_interrogation_interrogation(left, right)
- return left and (left * right) or right;
- end
- local function __saturnus_operator_slash_interrogation(left, right)
- return left and (left / right);
- end
- local function __saturnus_operator_slash_interrogation_interrogation(left, right)
- return left and (left / right) or right;
- end
- local function forward_iterator(from, to, step)
- local i = from;
- return function()
- if i > to then
- return nil;
- end
- local c = i;
- i = i + step;
- return c;
- end;
- end
- local function backward_iterator(from, to, step)
- local i = from;
- return function()
- if i < to then
- return nil;
- end
- local c = i;
- i = i - step;
- return c;
- end;
- end
- local function __saturnus_operator_dot_dot(from, target)
- local to, step;
- do
- local __destructure__ = (function()
- if type(target) == "table" then
- return target;
- end
- return {_0 = target, _1 = 1};
- end)();
- to = __destructure__._0;
- step = __destructure__._1;
- end
- if from < to then
- return forward_iterator(from, to, step);
- end
- return backward_iterator(from, to, step);
- end
- local function __saturnus_operator_minus_greater_greater(self, method)
- return method(self);
- end
- return {Object = Object, Tuple = Tuple, Vector = Vector, abstract = abstract, trait = trait, impl = impl, mixin = mixin, panic = panic, rtti = rtti, operators = {__saturnus_operator_colon_colon_colon = __saturnus_operator_colon_colon_colon, __saturnus_operator_dot_dot = __saturnus_operator_dot_dot, __saturnus_operator_pipe_greater = __saturnus_operator_pipe_greater, __saturnus_operator_less_pipe = __saturnus_operator_less_pipe, __saturnus_operator_plus_interrogation = __saturnus_operator_plus_interrogation, __saturnus_operator_plus_interrogation_interrogation = __saturnus_operator_plus_interrogation_interrogation, __saturnus_operator_plus_plus_interrogation = __saturnus_operator_plus_plus_interrogation, __saturnus_operator_plus_plus_interrogation_interrogation = __saturnus_operator_plus_plus_interrogation_interrogation, __saturnus_operator_minus_interrogation = __saturnus_operator_minus_interrogation, __saturnus_operator_minus_interrogation_interrogation = __saturnus_operator_minus_interrogation_interrogation, __saturnus_operator_times_interrogation = __saturnus_operator_times_interrogation, __saturnus_operator_times_interrogation_interrogation = __saturnus_operator_times_interrogation_interrogation, __saturnus_operator_slash_interrogation = __saturnus_operator_slash_interrogation, __saturnus_operator_slash_interrogation_interrogation = __saturnus_operator_slash_interrogation_interrogation, __saturnus_operator_colon_colon = __saturnus_operator_colon_colon, __saturnus_operator_minus_greater_greater = __saturnus_operator_minus_greater_greater}, ext = ext};
- end;
- if jit then
- package.loaded["std"] = _G["__saturnus_module_std"]();
- else
- package.preload["std"] = _G["__saturnus_module_std"];
- end
- -- Generated by the Saturnus compiler 1.0
- -- WARNING! Changes may be discarded at any moment!
- local __saturnus_spread__ = table.unpack or unpack; -- Lua polyfill
- local ev, Promise;
- do
- local __destructure__ = require("tokio");
- ev = __destructure__.ev;
- Promise = __destructure__.Promise;
- end
- local Ok, Err;
- do
- local __destructure__ = require("result");
- Ok = __destructure__.Ok;
- Err = __destructure__.Err;
- end
- local Object;
- do
- local __destructure__ = require("std");
- Object = __destructure__.Object;
- end
- local NoModemError = {};
- NoModemError.__meta__ = {};
- NoModemError.__meta__.__call = function(self, struct)
- return setmetatable(struct, self.prototype.__meta__);
- end;
- NoModemError.prototype = {};
- NoModemError.prototype.__proto__ = NoModemError;
- NoModemError.prototype.__meta__ = {};
- NoModemError.prototype.__meta__.__index = NoModemError.prototype;
- setmetatable(NoModemError, NoModemError.__meta__);
- NoModemError.prototype.to_string = function(self)
- return "No modems attached to this computer!";
- end;
- local ProtocolAlreadyExistsError = {};
- ProtocolAlreadyExistsError.__meta__ = {};
- ProtocolAlreadyExistsError.__meta__.__call = function(self, struct)
- return setmetatable(struct, self.prototype.__meta__);
- end;
- ProtocolAlreadyExistsError.prototype = {};
- ProtocolAlreadyExistsError.prototype.__proto__ = ProtocolAlreadyExistsError;
- ProtocolAlreadyExistsError.prototype.__meta__ = {};
- ProtocolAlreadyExistsError.prototype.__meta__.__index = ProtocolAlreadyExistsError.prototype;
- setmetatable(ProtocolAlreadyExistsError, ProtocolAlreadyExistsError.__meta__);
- ProtocolAlreadyExistsError.new = function(proto)
- return ProtocolAlreadyExistsError({proto = proto});
- end;
- ProtocolAlreadyExistsError.prototype.to_string = function(self)
- return "Protocol " .. tostring(self.proto) .. " already registered!";
- end;
- local ReservedHostNameError = {};
- ReservedHostNameError.__meta__ = {};
- ReservedHostNameError.__meta__.__call = function(self, struct)
- return setmetatable(struct, self.prototype.__meta__);
- end;
- ReservedHostNameError.prototype = {};
- ReservedHostNameError.prototype.__proto__ = ReservedHostNameError;
- ReservedHostNameError.prototype.__meta__ = {};
- ReservedHostNameError.prototype.__meta__.__index = ReservedHostNameError.prototype;
- setmetatable(ReservedHostNameError, ReservedHostNameError.__meta__);
- ReservedHostNameError.new = function(name)
- return ReservedHostNameError({name = name});
- end;
- ReservedHostNameError.prototype.to_string = function(self)
- return "Host name " .. tostring(self.name) .. " is reserved! Please, choose another.";
- end;
- local ProtocolDoesNotExistError = {};
- ProtocolDoesNotExistError.__meta__ = {};
- ProtocolDoesNotExistError.__meta__.__call = function(self, struct)
- return setmetatable(struct, self.prototype.__meta__);
- end;
- ProtocolDoesNotExistError.prototype = {};
- ProtocolDoesNotExistError.prototype.__proto__ = ProtocolDoesNotExistError;
- ProtocolDoesNotExistError.prototype.__meta__ = {};
- ProtocolDoesNotExistError.prototype.__meta__.__index = ProtocolDoesNotExistError.prototype;
- setmetatable(ProtocolDoesNotExistError, ProtocolDoesNotExistError.__meta__);
- ProtocolDoesNotExistError.new = function(name)
- return ProtocolDoesNotExistError({name = name});
- end;
- ProtocolDoesNotExistError.prototype.to_string = function(self)
- return "Protocol " .. tostring(self.name) .. " does not exist on this machine!";
- end;
- local function find_modem()
- local modem = peripheral.find("modem");
- if modem == nil then
- return Err(NoModemError({}));
- end
- return Ok(modem);
- end
- local function propagate(what)
- local ok, value;
- do
- local __destructure__ = {pcall(what)};
- ok = __destructure__[1];
- value = __destructure__[2];
- end
- if ok then
- return Ok(value);
- end
- return Err(value);
- end
- local Ether = {};
- Ether.__meta__ = {};
- Ether.__meta__.__call = function(self, struct)
- return setmetatable(struct, self.prototype.__meta__);
- end;
- Ether.prototype = {};
- Ether.prototype.__proto__ = Ether;
- Ether.prototype.__meta__ = {};
- Ether.prototype.__meta__.__index = Ether.prototype;
- setmetatable(Ether, Ether.__meta__);
- Ether.add_protocol = function(name, options)
- return propagate(function(it, ...)
- local rest = {...};
- local modem = find_modem():unwrap();
- modem.open(Ether.BCAST_PORT);
- options = options == nil and {} or options;
- if Ether.protocols[name] ~= nil then
- return Err(ProtocolAlreadyExistsError.new(name));
- end
- options.private = options.private == nil and false or options.private;
- options.channel = options.channel == nil and Ether.DEFAULT_COM or options.channel;
- modem.open(options.channel);
- Ether.protocols[name] = options;
- return Ok(nil);
- end);
- end;
- Ether.host = function(name)
- return propagate(function(it, ...)
- local rest = {...};
- local modem = find_modem():unwrap();
- modem.open(Ether.BCAST_PORT);
- if name == "localhost" then
- return Err(ReservedHostNameError.new(name));
- end
- Ether.hostname = name;
- return Ok(ev:on("modem_message", function(_, side, c, rc, message, distance)
- if c == Ether.BCAST_PORT then
- if message.body.request == "lookup" then
- Ether.broadcast({response = "lookup", name = Ether.hostname, protocols = Ether.protocols});
- end
- end
- end));
- end);
- end;
- Ether.lookup = function(timeout)
- return Promise.new(function(r)
- local hosts = {};
- local clear = ev:on("modem_message", function(_, side, c, rc, message, distance)
- if c == Ether.BCAST_PORT and message.body.response == "lookup" then
- hosts[message.body.name] = message.body.protocols;
- end
- end);
- local res = Ether.broadcast({request = "lookup"});
- if res.is_err() then
- r(res);
- else
- ev:timeout(function()
- clear();
- r(hosts);
- end, timeout == nil and Ether.DEFAULT_LOOKUP_TIMEOUT or timeout);
- end
- end);
- end;
- Ether.broadcast = function(body)
- return propagate(function(it, ...)
- local rest = {...};
- local modem = find_modem():unwrap();
- local sender_mac = Ether.MAC_ADDRESS;
- local sender = Ether.hostname;
- modem.transmit(Ether.BCAST_PORT, Ether.BCAST_PORT, {sender = sender, sender_mac = sender_mac, body = body});
- return Ok(nil);
- end);
- end;
- Ether.send = function(target, protocol, body)
- return propagate(function(it, ...)
- local rest = {...};
- local modem = find_modem():unwrap();
- local proto = Ether.protocols[protocol];
- if proto == nil then
- return Err(ProtocolDoesNotExistError.new(protocol));
- end
- local sender_mac = Ether.MAC_ADDRESS;
- local sender = Ether.hostname;
- local payload = {sender = sender, sender_mac = sender_mac, protocol = protocol, target = target, body = body};
- if type(proto.rewrite_transmit) == "function" then
- payload = proto.rewrite_transmit(payload);
- end
- modem.transmit(proto.channel, proto.channel, payload);
- return Ok(nil);
- end);
- end;
- Ether.on = function(protocol_name, callback)
- return ev:on("modem_message", function(_, side, c, rc, message, distance)
- if protocol_name ~= message.protocol then
- return;
- end
- local protocol = Ether.protocols[message.protocol];
- if protocol == nil then
- return;
- end
- if protocol.channel ~= c then
- return;
- end
- if type(protocol.rewrite_receive) == "function" then
- message = protocol.rewrite_receive(message);
- end
- if message.target ~= Ether.hostname and message.target ~= "*" then
- return;
- end
- callback(message.body, message);
- end);
- end;
- Ether.ev = ev;
- Ether.BCAST_PORT = 255;
- Ether.DEFAULT_COM = 254;
- Ether.protocols = {};
- Ether.DEFAULT_LOOKUP_TIMEOUT = 2;
- Ether.MAC_ADDRESS = os.getComputerID();
- return {Ether = Ether, NoModemError = NoModemError, ProtocolAlreadyExistsError = ProtocolAlreadyExistsError, ReservedHostNameError = ReservedHostNameError, ProtocolDoesNotExistError = ProtocolDoesNotExistError, find_modem = find_modem};
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement