Git Product home page Git Product logo

lua-in-js's Introduction

lua-in-js

npm PRs Welcome GitHub   Badges are clickable!

A Lua to JS transpiler/runtime. This library is a rewrite of Starlight with a lot of improvements.

Install

npm i lua-in-js

API

Import

const luainjs = require(luainjs)
// or
import luainjs from 'luainjs'

Create the lua environment

Lua environments are isolated from each other (they got different global scopes)

const luaEnv = luainjs.createEnv()

A config object can be passed in for extra functionality

const luaEnv = luainjs.createEnv({
    LUA_PATH,   // default value of package.path
    fileExists, // function that takes in a path and returns a boolean
    loadFile,   // function that takes in a path and returns the content of a file
    stdin,      // string representing the standard input
    stdout,     // function representing the standard output
    osExit      // function called by os.exit
})

Execute a script or file

const luaScript = luaEnv.parse('print(\'Hello world!\')')
const returnValue = luaScript.exec()
const luaScript = luaEnv.parseFile('somefile.lua')
const returnValue = luaScript.exec()

parseFile uses config.fileExists and config.loadFile

Create a global library

Creating a global library allows you write APIs that you can use in the Lua environment.

function helloBuilder(name) {
    const NAME = luainjs.utils.coerceArgToString(name, 'sayHi', 1)
    return `Hello ${NAME}!`
}

const myLib = new luainjs.Table({ helloBuilder })
luaEnv.loadLib('myLib', myLib)

const helloStr = luaEnv.parse(`return myLib.helloBuilder('John')`).exec()
console.log(helloStr)

Check out the math lib for a more extensive example.

Example

Check out the test runner for a concrete example.

Missing functionality

  • coroutine library
  • debug library
  • utf8 library
  • io library
  • package.cpath
  • package.loadlib
  • string.dump
  • string.pack
  • string.packsize
  • string.unpack
  • os.clock
  • os.execute
  • os.getenv
  • os.remove
  • os.rename
  • os.tmpname

lua-in-js's People

Contributors

teoxoy avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.