-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #277 from permaweb/twilson63/chg-dev-cli-update-in…
…it-274 refactor(dev-cli): added ao lib to loader.lua #274
- Loading branch information
Showing
3 changed files
with
161 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
local ao = { _version = "0.0.3", id = "", _module = "", authorities = {}, outbox = { Messages = {}, Spawns = {}, _ref = 0 } } | ||
|
||
function isArray(table) | ||
if type(table) == "table" then | ||
local maxIndex = 0 | ||
for k, v in pairs(table) do | ||
if type(k) ~= "number" or k < 1 or math.floor(k) ~= k then | ||
return false -- If there's a non-integer key, it's not an array | ||
end | ||
maxIndex = math.max(maxIndex, k) | ||
end | ||
-- If the highest numeric index is equal to the number of elements, it's an array | ||
return maxIndex == #table | ||
end | ||
return false | ||
end | ||
|
||
function ao.init(env) | ||
if ao.id == "" then | ||
ao.id = env.Process.Id | ||
end | ||
|
||
if ao._module == "" then | ||
for _, o in ipairs(env.Process.Tags) do | ||
if o.name == "Module" then | ||
ao._module = o.value | ||
end | ||
end | ||
end | ||
|
||
if #ao.authorities < 1 then | ||
for _, o in ipairs(env.Process.Tags) do | ||
if o.name == "Authority" then | ||
table.insert(ao.authorities, o.value) | ||
end | ||
end | ||
end | ||
|
||
ao.outbox = { Messages = {}, Spawns = {} } | ||
ao.env = env | ||
|
||
end | ||
|
||
-- clears outbox | ||
function ao.clearOutbox() | ||
ao.outbox = { Messages = {}, Spawns = {} } | ||
end | ||
|
||
function ao.send(msg) | ||
assert(type(msg) == 'table', 'msg should be a table') | ||
|
||
local message = { | ||
Target = msg.Target, | ||
Data = msg.Data, | ||
Tags = { | ||
{ name = "Data-Protocol", value = "ao" }, | ||
{ name = "Variant", value = "ao.TN.1"}, | ||
{ name = "Type", value = "Message" }, | ||
{ name = "From-Process", value = ao.id }, | ||
{ name = "From-Module", value = ao._module } | ||
} | ||
} | ||
|
||
if msg.Tags then | ||
if isArray(msg.Tags) then | ||
for _, o in ipairs(msg.Tags) do | ||
table.insert(message.Tags, o) | ||
end | ||
else | ||
for k,v in pairs(msg.Tags) do | ||
table.insert(message.Tags, { name = k, value = v }) | ||
end | ||
end | ||
end | ||
|
||
-- add message to outbox | ||
table.insert(ao.outbox.Messages, message) | ||
|
||
return message | ||
end | ||
|
||
function ao.spawn(module, msg) | ||
assert(type(module) == "string", "module source id is required!") | ||
assert(type(msg) == 'table', 'msg should be a table') | ||
-- inc spawn reference | ||
ao._ref = ao._ref + 1 | ||
|
||
if not msg.Data then | ||
data = "NODATA" | ||
end | ||
|
||
local spawn = { | ||
Data = data, | ||
Tags = { | ||
{ name = "Data-Protocol", value = "ao" }, | ||
{ name = "Variant", value = "ao.TN.1"}, | ||
{ name = "Type", value = "Process" }, | ||
{ name = "From-Process", value = ao.id }, | ||
{ name = "From-Module", value = ao._module }, | ||
{ name = "Module", value = module }, | ||
{ name = "Ref_", value = ao._ref } | ||
} | ||
} | ||
|
||
if msg.Tags then | ||
if isArray(msg.Tags) then | ||
for _, o in ipairs(msg.Tags) do | ||
table.insert(spawn.Tags, o) | ||
end | ||
else | ||
for k,v in pairs(msg.Tags) do | ||
table.insert(spawn.Tags, { name = k, value = v }) | ||
end | ||
end | ||
end | ||
|
||
-- add spawn to outbox | ||
table.insert(ao.outbox.Spawns, spawn) | ||
|
||
return spawn | ||
end | ||
|
||
function ao.isTrusted(msg) | ||
if #ao.authorities == 0 then | ||
return true | ||
end | ||
|
||
for _, authority in ipairs(ao.authorities) do | ||
if msg.From == authority then | ||
return true | ||
end | ||
if msg.Owner == authority then | ||
return true | ||
end | ||
end | ||
return false | ||
end | ||
|
||
function ao.result(result) | ||
return { | ||
Output = result.Output or '', | ||
Messages = ao.outbox.Messages, | ||
Spawns = ao.outbox.Spawns | ||
} | ||
end | ||
|
||
return ao |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters