https://github.com/liatemplates/lua
Lua interpreter and VM to be used in LiaScript for teaching programming
https://github.com/liatemplates/lua
coding liascript liascript-template lua oer
Last synced: 8 months ago
JSON representation
Lua interpreter and VM to be used in LiaScript for teaching programming
- Host: GitHub
- URL: https://github.com/liatemplates/lua
- Owner: LiaTemplates
- Created: 2019-11-27T13:50:28.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-11-27T13:51:20.000Z (over 6 years ago)
- Last Synced: 2025-01-17T12:16:34.374Z (about 1 year ago)
- Topics: coding, liascript, liascript-template, lua, oer
- Homepage: https://liascript.github.io/course/?https://raw.githubusercontent.com/liaTemplates/Lua/master/README.md
- Size: 1000 Bytes
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Lua - Template
--{{0}}--
This document defines some basic macros for applying the JavaScript Lua
interpreter and VM https://daurnimator.github.io in
[LiaScript](https://LiaScript.github.io) to make Markdown code-blocks
executable.
__Try it on LiaScript:__
https://liascript.github.io/course/?https://raw.githubusercontent.com/liaTemplates/Lua/master/README.md
__See the project on Github:__
https://github.com/liaTemplates/Lua
--{{1}}--
There are three ways to use this template. The easiest way is to use the
`import` statement and the URL of the raw text-file of the master branch or any
other branch or version. But you can also copy the required functionality
directly into the header of your Markdown document, see therefor the
[last slide](#3). And of course, you could also clone this project and change
it, as you wish.
{{1}}
1. Load the macros via
`import: https://raw.githubusercontent.com/liaTemplates/Lua/master/README.md`
2. Copy the definitions into your Project
3. Clone this repository on GitHub
## `@LUA.eval`
--{{0}}--
Add the macro `@LUA.eval` to the end of a single Lua code-block to make it
executable and editable in LiaScript. The current code gets evaluate by the
JavaScript Lua interpreter and the result is shown in a console below.
``` lua
print('hello' .. ' ' .. 'world!') -- This is Lua!
print(js.global:eval('[0,1,2,3,4,5][3]')) -- Run JS from Lua
-- Interact with the page using Lua
local screen = js.global.screen
print("you haz " .. (screen.width*screen.height) .. " pixels")
local window = js.global -- global object in JS is the window
window:alert("hello from lua!")
window:setTimeout(function() print('hello from lua callback') end, 2500)
local document = js.global.document
print("this window has title '" .. document.title .. "'")
-- call constructors (global, or as properties of other objects)
print("i made an ArrayBuffer of size " .. js.new(js.global.ArrayBuffer, 20).byteLength)
-- print("i made an ArrayBuffer of size " .. js.global.ArrayBuffer:new(20).byteLength)
print("time iz " .. js.global.Date.now()) -- call with no arguments
print('done!')
```
@Lua.eval
## Implementation
--{{0}}--
The code shows how the macro is implemented by calling the macro `@Lua.eval`.
The `@input` macro gets substituted by the current input code. The script
commands at the top define the references to the Lua JavaScript implementations
that need to be called to load the interpreter and VM.
``` js
script: https://daurnimator.github.io/lua.vm.js/js/lua.js
https://daurnimator.github.io/lua.vm.js/lua.vm.js
@Lua.eval
emscripten.print = function(x) {
console.log(x);
}
L.execute(`@input`)
@end
```
--{{1}}--
If you want to minimize loading effort in your LiaScript project, you can also
copy this code and paste it into your main comment header, see the code in the
raw file of this document.
{{1}} https://raw.githubusercontent.com/liaTemplates/Lua/master/README.md