Git Product home page Git Product logo

json-lua's Introduction

Fengari Demo

This project is a demo of using the Fengari library to talk to a lua environment within the browser.

It comes with a dump.lua file and a json.lua file which are both loaded inside the hook

The hook then allows code to be executed and can be used for state management or ser/des.

//src/useLuaEnv.ts

import axios from "axios";
import * as fengari from "fengari-web";
import { useAsync } from "react-use";

/**
 * The list of Lua modules to load.
 */
const luaModules = ["/json.lua", "/dump.lua"];

/**
 * Function to create a Lua executor. This executor is able to run Lua scripts by first loading required Lua modules.
 * @async
 * @returns {Promise<Function>} A Promise that resolves to a function which can execute Lua scripts.
 */
const getLuaExecutor = async () => {
    const modules = await Promise.all(
        luaModules.map((it) => axios.get<string>(it).then((it) => it.data))
    );
    modules.forEach((luaModule) => fengari.load(luaModule)());

    return (luaCode: string) => fengari.load(luaCode)();
};

/**
 * Hook to interact with the Lua environment.
 * @returns {object} An object containing the execute function, loading state, potential error, getLuaString function and getJsonString function.
 */
export const useLuaEnv = () => {
    const { value: execute, loading, error } = useAsync(getLuaExecutor, []);

    /**
     * Function to get a Lua string representation from a JSON string.
     * @param {string} json - The JSON string.
     * @returns {string|undefined} The Lua string representation or undefined if the executor is not yet loaded.
     */
    const getLuaString = (json: string) =>
        execute?.(`return dump(json.decode('${json}'))`);

    /**
     * Function to get a JSON string from a Lua table string.
     * @param {string} luaTable - The Lua table string.
     * @returns {string|undefined} The JSON string or undefined if the executor is not yet loaded.
     */
    const getJsonString = (luaTable: string) =>
        execute?.(`return json.encode(${luaTable})`);

    return { execute, loading, error, getLuaString, getJsonString };
};

You can then use the hook to execute lua, or just use the utility actions to convert JSON and lua back and forth.

// src/App.tsx
import { AppShell, Group } from "@mantine/core";
import { Prism } from "@mantine/prism";
import { useLuaEnv } from "./useLuaEnv.ts";

const App = () => {
  const luaEnv = useLuaEnv();

  return (
    <AppShell>
      {luaEnv.execute && (
        <Group grow>
          <Prism title={"Hello"} language="json">
            {luaEnv.getJsonString(`{ [1] = 1,[2] = 2,[3] = 3,[4] = { ["x"] = 10 } }`)}
          </Prism>
          <Prism language="json">
            {luaEnv.getLuaString(`[1,2,3,{"x":10}]`)}
          </Prism>
        </Group>
      )}
    </AppShell>
  );
};

export default App;

json-lua's People

Watchers

 avatar  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.