Git Product home page Git Product logo

castl's People

Contributors

paulbernier avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

castl's Issues

Transpilation error on valid JS code with identifiers similar to reserved words.

Valid JS code

export {
    map,
    void
} from "../Data.Functor/index.js";

causes transpilation error:

Control.Apply
/home/renegatto/castl/bin/castl.js:170
        throw new SyntaxError("Couldn't transpile JS code: " + e);
        ^

SyntaxError: Couldn't transpile JS code: SyntaxError: ./output/Control.Apply/index.js: void is a reserved word (127:4)

The same happens with many of the reserved words.
For example: delete,new,while.

Bootstrapping?

I was looking for a library which would assist in creating an embedded javascript environment in the Minecraft Mod ComputerCraft (in particular, its derivative, CC:Tweaked), which executes programs written in lua. This library is almost perfect, though I cannot find a way to bootstrap it into pure lua. Would it be possible to create a small bootstrap program that basically runs this through itself, once to go to lua, once to check that it still works, and once again to confirm that the results are reproduceable, and the result being the final version (the one which was compiled to confirm reporduceability).
Alternatively, I don't particularily need an automated method of doing so, if it is already possible to have the compiler compile itself in some way.

no such file or directory: code.js

I have a JS file named contiguous.js that I want to convert to Lua. Running castl -o contiguous.js gives this error:

Daniel@LAPTOP-J4LVCVKU MINGW64 ~/Desktop/repos/mine/contiguous-2d
$ castl -o contiguous.js
C:\Users\Daniel\AppData\Roaming\npm\node_modules\castl\bin\castl.js:170
        throw new SyntaxError("Couldn't transpile JS code: " + e);
        ^

SyntaxError: Couldn't transpile JS code: Error: ENOENT: no such file or directory, open 'C:\Users\Daniel\Desktop\repos\mine\contiguous-2d\code.js'
    at C:\Users\Daniel\AppData\Roaming\npm\node_modules\castl\bin\castl.js:170:15
    at Object.<anonymous> (C:\Users\Daniel\AppData\Roaming\npm\node_modules\castl\bin\castl.js:292:2)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:394:7)
    at startup (bootstrap_node.js:149:9)

Why is it looking for code.js when I ran it on contiguous.js? What am I missing? I am on Windows 10.

No compatible version found: [email protected]

When i npm install i got the following err:

npm ERR! No compatible version found: [email protected]
npm ERR! Valid install targets:
npm ERR! 6.6.0, 6.5.0, 6.5.0-1, 6.3.13, 6.2.4, 6.1.18, 6.1.17, 6.1.4, 6.1.2, 6.0.15, 6.0.14, 6.0.12, 6.0.11, 6.0.8, 6.0.2

With the line "babel-preset-es2015": "6.6.0", all installed successfully.

Offtopic: whether it is possible to use this tool to work livit? (ie for porting modules nodejs)
And thank you so much for wonderful perspective ))

Bundling runtime

Is it possible to bundle runtime into the output file and to not carry around it with a project?

Unable to recursively call named lambda

function outer()
{
    return function inner(x) {
         if(x == 10)
              return x;
         return inner.call(x, x+1);
    }(0);
}
console.log("Done: " + outer());

Any thoughts? I tried to implement it, but ended up butchering it :)

KaTeX compiles, but running fails with Lua error "too many C levels (limit is 200)"

I tried translating KaTeX (v0.7.1 from git) with castl (1.2.4). The translation "succeeded", with commands roughly as below:

$ cd katex
$ git checkout v0.7.1
$ for f in katex.js src/*.js; do castl $f -o --debug --node; done

However, trying to run the following Lua script afterwards with Lua 5.2:

local katex = assert(loadfile 'katex.js.lua')()

failed with a Lua error: [...] too many C levels (limit is 200) [...], which seems to mean there's too deep nesting in the code.

I'm aware that castl is an experimental project, but I'm reporting this problem just in case you (or someone else) are crazy enough to try fixing the issue somehow :)

Guide for transpiling bigger babel based js projects

What would be the general steps in transpiling a bigger project like https://github.com/graphql/graphql-js
Would it be enough to change this line in order to have the basis for converting most of the project?
Is the idea to transpile one file at a time (using some bash script to automate)
Although not an expert in any of the languages i am comfortable with changing any part (modify/anotate the js project, modify castle code).

Thank you

Too many local variables (limit is 200)

Hello,

I'm trying to get a fully featured RegEx library for Lua that will run in a pure Lua environment (no require, no FFI, no os or io packages, etc.) -- specifically the Lua 5.1 interpreter baked into Elder Scrolls Online.

For this I've set out to try to use onigurumajs, browserify, castl, and node-lua-distiller to get onigurumajs into a single Lua file with no dependencies.

Here's what I did, exactly (on macOS, but should work plus or minus a sudo on Linux, and probably even Windows Git Bash):

#!/bin/bash
set -e
rm -rf ooftest
mkdir -p ooftest
pushd ooftest
git clone https://github.com/bcoe/onigurumajs
git clone https://github.com/PaulBernier/castl
cd castl
git apply --ignore-whitespace << 'EOF'
diff --git a/lua/castl/constructor/date.lua b/lua/castl/constructor/date.lua
index e7faa74..302342e 100644
--- a/lua/castl/constructor/date.lua
+++ b/lua/castl/constructor/date.lua
@@ -92,29 +92,10 @@ end
 
 Date._timestamp = 0
 
-if luajit then
-    local ffi = require("ffi")
-    -- posix systems only
-    ffi.cdef[[
-        typedef struct timeval {
-          long tv_sec;
-          long tv_usec;
-        } timeval;
-        int gettimeofday(struct timeval *restrict tp, void *restrict tzp);
-    ]]
-
-    local te = ffi.new("timeval[1]")
-
-    Date.now = function(this)
-        ffi.C.gettimeofday(te, nil);
-        return tonumber(te[0].tv_sec * 1000 + te[0].tv_usec / 1000);
-    end
-else
     Date.now = function(this)
         -- TODO: write a C function to get milliseconds
         return time() * 1000
     end
-end
 
 Date.parse = function(this, str)
     -- TODO: parse RFC2822 only for now
diff --git a/lua/castl/modules/dkjson.lua b/lua/castl/modules/dkjson.lua
index a1c7c69..1468c9e 100644
--- a/lua/castl/modules/dkjson.lua
+++ b/lua/castl/modules/dkjson.lua
@@ -69,7 +69,7 @@ local _ENV = nil -- blocking globals in Lua 5.2
 pcall (function()
     -- Enable access to blocked metatables.
     -- Don't worry, this module doesn't change anything in them.
-    local debmeta = require "debug".getmetatable
+    -- local debmeta = require "debug".getmetatable
     if debmeta then getmetatable = debmeta end
 end)
 

EOF
cd ../onigurumajs
npm install -g browserify castl luamin npm-check-updates coffeescript
ncu xregexp -u
npm install
npm install lua-distiller
browserify index.js --bare > oniguruma.js
cp -rf ../castl/lua/castl .
castl oniguruma.js --babel --mini -o oniguruma.lua
./node_modules/lua-distiller/bin/lua-distiller.coffee -i oniguruma.lua -o oniguruma-all.lua
luamin -f oniguruma-all.lua  > oniguruma-all-min.lua
lua -i oniguruma-all-min.lua

The error:

$ lua -i oniguruma-all-min.lua
Lua 5.3.5  Copyright (C) 1994-2018 Lua.org, PUC-Rio
lua: oniguruma-all-min.lua:1: too many local variables (limit is 200) in function at line 1 near ','

or, unminified:

$ lua -i oniguruma-all.lua
Lua 5.3.5  Copyright (C) 1994-2018 Lua.org, PUC-Rio
lua: oniguruma-all.lua:25330: too many local variables (limit is 200) in function at line 25329 near ','

Line 25330 is basically a bunch of variable declarations inside the lodash library that is required by onigurumajs. The number of variable declarations definitely exceeds 200, hence the error.

Notes about stuff in the script:

  • The castl patch is necessary both because lua-distiller doesn't find the ffi and debug modules (even when running fully privileged lua 5.3.5 locally), and because those modules aren't available in Elder Scrolls Online's extremely locked down lua interpreter anyway.
  • Upgrading to xregexp 4.x using ncu was necessary to resolve a problem with browserify being unable to browserify the xregexp addons in version 3.x which oniguruma was asking for.
  • I've tried this both with, and without minification, both on the castl side and the lua side.

There may still be deeper errors once these are resolved, but the issue with 200 variables must be resolved first.

Somehow your Lua code generator needs to detect >200 local variables in a scope, and stuff them into a table, then rewrite all the uses of them to use a table instead of direct local variable references. That sounds like a non-trivial effort, but anything that includes the lodash library, for starters, can't be used with castl as-is.

FWIW, tessel has the same problem :( tessel/t1-runtime#658

I will be "happy" (as far as castl is concerned) if the transpiled Lua code works with a desktop Lua interpreter, e.g. the reference implementation as available on MacOS or Linux. But I have a feeling I will hit a few more issues based on the limitations of the Havok Script Lua engine of ESO, which you can read more about here in case you're interested: https://wiki.esoui.com/Esolua

success story with ASCIIMathML.js โ€” thanks! :)

Wanted to write by email, but given that there are two small tweaks worth reporting, I decided to write via github.

So, firstly, big thanks for the tool! :) I used it to successfully translate the ASCIIMathML.js tool from JS to Lua 5.2. I'm hoping to use it to build a math rendering feature into the SILE typesetter.

As an aside, I had to do two tiny but really tricky tweaks after the initial port:

  1. x = {} (function() end) is treated by Lua as an attempt to call an array; I had to insert a comma ; to fix such a situation and it took me some time to realize this was the reason for a weird Lua error message;
  2. castl.sort is potentially slightly broken when translating JS sort to Lua table.sort โ€” Lua expects the compare func to implement <, while castl.sort resulted in <= semantics in my case. I tweaked the translated code to fix the issue in my code, as it was easier for me in this case than tweaking castl.sort.

Thanks again!

EDIT: added link to the fix I did regarding castl.sort; forgot to add the link originally :/

Compile failure

Errors found while trying to compile castl with castl:

shebang line not ignored. results in:

/opt/castl/lua$ ../bin/castl.js  ../bin/castl.js

/opt/castl/bin/castl.js:158
        throw new SyntaxError("Couldn't transpile JS code: " + e);
              ^
SyntaxError: Couldn't transpile JS code: Error: Line 1: Unexpected token ILLEGAL
    at /opt/castl/bin/castl.js:158:15
    at Object.<anonymous> (/opt/castl/bin/castl.js:280:2)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:906:3

Increment duplicated to run twice instead of once

I have the following JS in mcve.js:

var bits = [0,1,1,1];
var i = 3;
console.log(bits[++i] && i % 2 !== 0);
console.log(i);

I run castl mcve.js -o, then lua52 mcve.js.lua.

This logs undefined then 5.

If I run the same JS in Chrome I get undefined then 4.

Here are the contents of mcve.js.lua:

local _ENV = require("castl.runtime");
local i,bits;

bits = _arr({[0]=0,1,1,1},4);
i = 3;
console:log(((function() if _bool(bits[(function () local _tmp = _inc(i); i = _tmp; return _tmp; end)()]) then return ((_mod(i,2)) ~= 0);  else return bits[(function () local _tmp = _inc(i); i = _tmp; return _tmp; end)()];  end end)()));
console:log(i);

This part with the increment appears twice in the Lua output:

bits[(function () local _tmp = _inc(i); i = _tmp; return _tmp; end)()]

Fallback to functions _get and _set if types are unknown

Indexing and indexed assignment have slightly different semantics in JS vs Lua. This will also help with #3.

Instead of compiling a = foo.bar to a = foo.bar, it should instead compile to a = _get(foo, "bar").

I had an attempt at this locally, and got it working for indexing and for method calls, but not for assignment; i.e. for a.b = c I got _get(a, "b") = c which is invalid lua code. I think getting this right will require changes to all the compile{Assignment,Update}Expression* functions

Is it possible to remove the import of the runtime library?

The transpiler inserts a import of castl.runtime at the top and uses _ENV in alot of places, is it possible to get rid of this ? the environment where i do these things i don't need the extra dependencies, it will already be a sort of controlled environment, just need the syntax

String.prototype.replace() without RegExp

The first argument of String.prototype.replace() is actually interpreted as a Lua search pattern, not as a string.
For example, this code
console.log("String.prototype.replace()".replace("$", " is buggy"))
prints String.prototype.replace() is buggy, while it should just print String.prototype.replace().

How to setmetatable for an Object

I want override the default toString in both lua and js.

So normally I could do that in JavaScript with the Object.setPrototypeOf function, which finally call Object.defineProperty after babel-transformation, to change the proto chain .

However, I found that castl defines the Object.defineProperty with rawset, and also does not expose the setmetatable function.

So I'd like to ask how can I make use of the custom toString and __tostring in both lua and javascript

Unable to run (paths issue?)

Trying to run without linking:

$ ./bin/castl.js 
lua5.2: cannot open ./.code.js.lua: No such file or directory

Trying to run after npm link:

$ castl 
lua5.2: ./.code.js.lua:1: module 'castl.runtime' not found:
	no field package.preload['castl.runtime']
	no file '/usr/share/lua/5.2/castl/runtime.lua'
	no file '/usr/share/lua/5.2/castl/runtime/init.lua'
	no file '/usr/lib/lua/5.2/castl/runtime.lua'
	no file '/usr/lib/lua/5.2/castl/runtime/init.lua'
	no file './castl/runtime.lua'
	no file '/home/daurnimator/.luarocks/share/lua/5.2/castl/runtime.lua'
	no file '/home/daurnimator/.luarocks/share/lua/5.2/castl/runtime/init.lua'
	no file '/usr/share/lua/5.2/castl/runtime.lua'
	no file '/usr/share/lua/5.2/castl/runtime/init.lua'
	no file '/usr/lib/lua/5.2/castl/runtime.so'
	no file '/usr/lib/lua/5.2/loadall.so'
	no file './castl/runtime.so'
	no file '/home/daurnimator/.luarocks/lib/lua/5.2/castl/runtime.so'
	no file '/usr/lib/lua/5.2/castl/runtime.so'
	no file '/usr/lib/lua/5.2/castl.so'
	no file '/usr/lib/lua/5.2/loadall.so'
	no file './castl.so'
	no file '/home/daurnimator/.luarocks/lib/lua/5.2/castl.so'
	no file '/usr/lib/lua/5.2/castl.so'
stack traceback:
	[C]: in function 'require'
	./.code.js.lua:1: in main chunk
	[C]: in ?

After cd lua:

 $ castl 
/home/daurnimator/src/castl/bin/castl.js:170
        throw new SyntaxError("Couldn't transpile JS code: " + e);
        ^

SyntaxError: Couldn't transpile JS code: Error: ENOENT: no such file or directory, open 'code.js'
    at /home/daurnimator/src/castl/bin/castl.js:170:15
    at Object.<anonymous> (/home/daurnimator/src/castl/bin/castl.js:292:2)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:425:7)
    at startup (bootstrap_node.js:146:9)

module 'castl.runtime' not found

I am new to castl and lua. I compiled a JS file to lua 5.2 with castl -o.

This is the first line of my new lua file:

local _ENV = require("castl.runtime");

I tried running that file with lua52 script.js.lua, but I got this error:

C:\Program Files\lua-5.2.4_Win64_bin\lua52.exe: script.js.lua:1: module 'castl.runtime' not found:
        no field package.preload['castl.runtime']
        no file 'C:\Program Files\lua-5.2.4_Win64_bin\lua\castl\runtime.lua'
        no file 'C:\Program Files\lua-5.2.4_Win64_bin\lua\castl\runtime\init.lua'
        no file 'C:\Program Files\lua-5.2.4_Win64_bin\castl\runtime.lua'
        no file 'C:\Program Files\lua-5.2.4_Win64_bin\castl\runtime\init.lua'
        no file '.\castl\runtime.lua'
        no file 'C:\Program Files\lua-5.2.4_Win64_bin\castl\runtime.dll'
        no file 'C:\Program Files\lua-5.2.4_Win64_bin\loadall.dll'
        no file '.\castl\runtime.dll'
        no file 'C:\Program Files\lua-5.2.4_Win64_bin\castl\runtime52.dll'
        no file '.\castl\runtime52.dll'
        no file 'C:\Program Files\lua-5.2.4_Win64_bin\castl.dll'
        no file 'C:\Program Files\lua-5.2.4_Win64_bin\loadall.dll'
        no file '.\castl.dll'
        no file 'C:\Program Files\lua-5.2.4_Win64_bin\castl52.dll'
        no file '.\castl52.dll'
stack traceback:
        [C]: in function 'require'
        script.js.lua:1: in main chunk
        [C]: in ?

What am I missing? I am on Windows 10.

Emulate node's require

It would be nice to be able to have lua load node.js modules on the fly.
i.e. runtime:require("../foo.js").

  • It should take care of relative paths etc.
  • It should create the node.js module object
  • It should interpret package.json to find the 'main' module.

SyntaxError: Couldn't transpile JS code: Error: Couldn't find preset "es2015" relative to directory "."

When I try to transpile my ES6 code, I got the error message saying that

SyntaxError: Couldn't transpile JS code: Error: Couldn't find preset "es2015" relative to directory "."

Then I try to solve this problem by using this solution

And I try to revise the code from "presets": ["es2015"] to "presets": ['babel-preset-es2015'].map(require.resolve)

And which was introduced from here

And it works

Can't compile and run React.JS

@zeen thought it would be cool to compile React.JS into Lua.
The JS source file: http://fb.me/react-0.11.1.js

The first error is

lua: ./castl/prototype/regexp.lua:110: bad argument #1 to 'find' (string, table or userdata expected, got boolean)
stack traceback:
        [C]: in function 'find'
        ./castl/prototype/regexp.lua:110: in function 'test'
        react-0.11.1.lua:4840: in function 'inject'
        react-0.11.1.lua:2388: in function <react-0.11.1.lua:2367>
        (...tail calls...)
        react-0.11.1.lua:37: in function 's'
        react-0.11.1.lua:48: in function <react-0.11.1.lua:14>
        react-0.11.1.lua:14: in function 'e'
        react-0.11.1.lua:9: in function <react-0.11.1.lua:9>
        react-0.11.1.lua:9: in function <react-0.11.1.lua:2>
        react-0.11.1.lua:2: in main chunk
        [C]: in ?

Which comes from https://github.com/facebook/react/blob/8cb2812cff6a6cb54bb673254f1170281ce0790a/src/browser/ui/ReactDefaultInjection.js#L117 I just set the conditional to false so I could continue; but there's something weird going on there.

The next error I get is

lua: react-0.11.1.lua:10: attempt to index upvalue 'f' (a nil value)
stack traceback:
        react-0.11.1.lua:10: in function <react-0.11.1.lua:10>
        react-0.11.1.lua:10: in function <react-0.11.1.lua:3>
        react-0.11.1.lua:3: in main chunk
        [C]: in ?

Which is in the module definition bit at the start.

Parinfer.js

I am trying to compile parinfer.js to lua. I am newbie in lua but it seems to be something wrong with the implementation of the split method from js to lua ?

lua: TypeError: Cannot read property 'split' of undefined
at (./castl/core_objects.lua:348)
at __index (./castl/core_objects.lua:296)
at getInitialResult (./parinfer-js.lua:95)
at processText (./parinfer-js.lua:1075)
at intentMode1 (./parinfer-js.lua:1152)
at (out.lua:14)
at ([C]:-1)

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.