swittk / react-native-lua

Lua Interpreter in React Native
MIT License
10 stars 3 forks source link

react-native-lua

Native Lua in React Native Heavily based on React Native JSI 😅. Working in both iOS and Android.

Lots of inspiration from ObjC-Lua and ilua.

The Lua source version (as of February 2022) is 5.4.4.

Installation

npm install react-native-lua

iOS

cd ios && pod install

Android

You need NDK installed. That's it.

Usage

Methods can be seen in the types.

Almost all methods are simply the same as known C api methods of the pattern lua_${methodName}, sans the lua_ prefix.

import { luaInterpreter } from "react-native-lua";
// Create a new interpreter
const interp = luaInterpreter();
// this is equivalent to lua_dostring
interp.dostring(`a = 2
b = a ^ 2
a = b * 20
    `);
// Or asynchronously! Each Lua Interpreter spawns its own thread when executing async code, so this doesn't block any other processes.
interp.dostringasync(`i = 0
while(i < 8)
do
  print(i)
  sleep(100)
  i = i + 1
end`)

// The rest is up to you!

The interpreter in action

the-interpreter-in-action

Execution limits

Since Lua is a scripting language, it wouldn't be nice if our code suddenly got stuck in a forever loop and blocks the whole program.

This library handles this condition by leveraging Lua runtime's lua_sethook function (which allows us to monitor the code execution every N commands).

The property executionLimit defines the number of milliseconds the script should run until it is terminated. Default value is 10000 (10 seconds). You can set this value by calling setExecutionLimit(ms)

Future plans

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

License

MIT


Tip Jar

If you appreciate my work, help buy me some soda 🥤 via the following routes.

Stellar
Stellar Lumens (XLM) : 
GCVKPZQUDXWVNPIIMF3FXR6KWAOHTEWPZZM2AQE4J3TXR6ZDHXQHP5BQ
Cardano
Cardano (ADA) : 
addr1q9datt8urnyuc2059tquh59sva0pja7jqg4nfhnje7xcy6zpndeesglqkxhjvcgdu820flcecjzunwp6qen4yr92gm6smssug8