-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(lua-examples): add hello world process example
- Loading branch information
1 parent
43a21a9
commit 0d47ffa
Showing
5 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,10 @@ | ||
{ | ||
"name": "hello-world-process", | ||
"version": "1.0.0", | ||
"license": "MIT", | ||
"type": "module", | ||
"main": "index.js", | ||
"dependencies": { | ||
"@permaweb/ao-loader": "^0.0.9" | ||
} | ||
} |
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,65 @@ | ||
-- Corresponding local wasm at ./process.wasm | ||
local JSON = require("json") | ||
|
||
local process = { _version = "0.0.6" } | ||
|
||
local function assoc(prop, val, obj) | ||
local result = {} | ||
for p, k in pairs(obj) do | ||
result[p] = k | ||
end | ||
result[prop] = val | ||
return result | ||
end | ||
|
||
local function findObject(array, key, value) | ||
for i, object in ipairs(array) do | ||
if object[key] == value then | ||
return object | ||
end | ||
end | ||
return nil | ||
end | ||
|
||
local function dump(o) | ||
if type(o) == 'table' then | ||
local s = '{ ' | ||
for k,v in pairs(o) do | ||
if type(k) ~= 'number' then k = '"'..k..'"' end | ||
s = s .. '['..k..'] = ' .. dump(v) .. ',' | ||
end | ||
return s .. '} ' | ||
else | ||
return tostring(o) | ||
end | ||
end | ||
|
||
local actions = {} | ||
actions['hello'] = function (state) | ||
local _state = assoc('heardHello', true, state) | ||
_state = assoc('helloCount', _state.helloCount + 1, _state) | ||
return { state = _state, output = nil } | ||
end | ||
actions['world'] = function (state) | ||
local _state = assoc('heardWorld', true, state) | ||
return { state = _state, output = nil } | ||
end | ||
actions['raw'] = function (state) | ||
return { state = state , output = JSON.encode(state) } | ||
end | ||
|
||
function process.handle(message, AoGlobal) | ||
if state == nil then state = { helloCount = 0 } end | ||
|
||
local func = findObject(message.tags, "name", "function") | ||
if func == nil then return error({ code = 500, message = 'no function tag in the message'}) end | ||
|
||
local res = actions[func.value](state, message, AoGlobal) | ||
state = res.state | ||
local output = res.output | ||
if (state.heardHello and state.heardWorld) then state = assoc('happy', true, state) end | ||
|
||
return { output = output } | ||
end | ||
|
||
return process |
Binary file not shown.
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,56 @@ | ||
import readline from 'node:readline' | ||
import fs from 'node:fs' | ||
|
||
import AoLoader from '@permaweb/ao-loader' | ||
|
||
const wasm = fs.readFileSync('./process.wasm') | ||
|
||
const rl = readline.createInterface({ | ||
input: process.stdin, | ||
output: process.stdout | ||
}) | ||
|
||
const env = { | ||
process: { | ||
id: 'PROCESS_TEST', | ||
owner: 'OWNER' | ||
} | ||
} | ||
|
||
async function repl (state) { | ||
const handle = await AoLoader(wasm) | ||
|
||
rl.question('hello world' + '> ', async function (line) { | ||
// Exit the REPL if the user types "exit" | ||
if (line === 'exit') { | ||
console.log('Exiting...') | ||
rl.close() | ||
return | ||
} | ||
// Evaluate the JavaScript code and print the result | ||
try { | ||
const message = createMessage(line) | ||
const { buffer, output } = handle(state, message, env) | ||
if (output) console.log(JSON.parse(output)) | ||
// Continue the REPL | ||
await repl(buffer) | ||
} catch (err) { | ||
console.log('Error:', err) | ||
process.exit(0) | ||
} | ||
}) | ||
} | ||
|
||
repl(null) | ||
|
||
function createMessage (expr) { | ||
return { | ||
owner: 'OWNER', | ||
target: 'PROCESS', | ||
tags: [ | ||
{ name: 'Data-Protocol', value: 'ao' }, | ||
{ name: 'ao-type', value: 'message' }, | ||
{ name: 'function', value: expr } | ||
] | ||
} | ||
} |