Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

lua-libloader

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

lua-libloader

A nodejs library wrapper for lua-in-js.

  • 1.0.6
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

lua-in-js-libloader

A nodejs library wrapper for lua-in-js.

npm

This is a little project of mine, which allows you to refrence nodejs functions in lua using lua-in-js by teoxoy.

It currently supports:

  • Constant variables
  • Functions
  • Classes (Atleast don't throw errors anymore)
  • async Functions / Promises
  • Events

Installation

npm i lua-libloader

Example

Heres an example on how to use it using os.platform():

//import stuff
var lua = require("lua-in-js")
var path = require("path");
var fs = require("fs");
var LibLoader = require("lua-libloader");

//create lua env
var luaEnv = lua.createEnv({
    LUA_PATH:__dirname,
    fileExists: p => fs.existsSync(path.join(__dirname, p)),
    loadFile: p => fs.readFileSync(path.join(__dirname, p), { encoding: 'utf8' }),
    osExit: code => (exitCode += code),
})

//load the libloader and make "os.platform" available for import.
luaEnv.loadLib("libloader",LibLoader([
    "os.platform"
]))

//execute a script which executes "os.platform"
var out = luaEnv.parse(`
local os = libloader.load("os")
local platform = os.platform()
libloader.clear()
return platform
`).exec();

//log the platform
console.log(out);

Here is an example using a Promise:

//import stuff
var lua = require("lua-in-js")
var path = require("path");
var fs = require("fs");
var LibLoader = require("lua-libloader");

//Define sleep function (Promise)
function sleep(ms) {
    return new Promise((res,rej)=>{
        setTimeout(res,ms);
    })
}
//create lua env
var luaEnv = lua.createEnv({
    LUA_PATH:__dirname,
    fileExists: p => fs.existsSync(path.join(__dirname, p)),
    loadFile: p => fs.readFileSync(path.join(__dirname, p), { encoding: 'utf8' }),
    osExit: code => (exitCode += code),
})

// Convert any function to a lib using ObjectToTable()
luaEnv.loadLib("time",LibLoader.ObjectToTable({now:Date.now}))
luaEnv.loadLib("wait",LibLoader.ObjectToTable({sleep}))

/*
execute a script which gets the time, 
executes sleep with 1000ms, and logs the time difference
*/
var out = luaEnv.parse(`
local timestart = time.now();
wait.sleep(1000);
return time.now() - timestart;
`).exec()

//log the time (1003ms on my pc)
console.log(out);

Keywords

FAQs

Package last updated on 30 Dec 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc