Skip to content

Commit

Permalink
feat(lua-examples): add hello world process example
Browse files Browse the repository at this point in the history
  • Loading branch information
TillaTheHun0 committed Nov 9, 2023
1 parent 43a21a9 commit 0d47ffa
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 0 deletions.
24 changes: 24 additions & 0 deletions lua-examples/hello-world/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions lua-examples/hello-world/package.json
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"
}
}
65 changes: 65 additions & 0 deletions lua-examples/hello-world/process.lua
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 added lua-examples/hello-world/process.wasm
Binary file not shown.
56 changes: 56 additions & 0 deletions lua-examples/hello-world/repl.js
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 }
]
}
}

0 comments on commit 0d47ffa

Please sign in to comment.