face-hh / webx

An alternative for the World Wide Web - browse websites such as buss://yippie.rizz made in HTML, CSS and Lua. Custom web browser, custom HTML rendering engine, custom search engine, and more.
Apache License 2.0
1.84k stars 155 forks source link

[napture] feat: Native JSON functions, require() #140

Closed nullsoepic closed 4 months ago

nullsoepic commented 4 months ago

This PR adds:

JSON

Can be just used as json.stringify() and json.parse(), no need to require/import anything

Time

Used as time(), returns seconds instead of millis like the standard lua os.time()

Require

Fetches, loads and executes some lua code from a url, and returns the output

Fetch

Fetch now supports responses that do not return JSON, for backwards compatibility and simplicity, a table will be returned if the response is valid JSON

jamiw1 commented 4 months ago

require should work with local/relative paths in addition to urls, and Time() should absolutely use milliseconds (preferably unix time) instead of just seconds

nullsoepic commented 4 months ago

require should work with local/relative paths in addition to urls, and Time() should absolutely use milliseconds (preferably unix time) instead of just seconds

I attempted to support relative paths (03006c8) but had a bunch of issues, still not sure how that could be implemented.

time already uses unix time, the reason for using seconds is compatibility with standard lua. We could have a argument/seperate function to get milliseconds

GStudiosX2 commented 4 months ago

Fetch already supported non Json ?

nullsoepic commented 4 months ago

My bad, looks like it does work, had an issue where github raw urls would just return a table with status 200 and no content so I assumed it just was not supported without looking at it enough.

Though the current implementation is a bit inconsistent, requests that return json will give you the json as a table, where as other requests that don't return json will give you a table with the raw content and status.

My implementation just gives you a table if its valid json, and a string with the raw response if it isn't. Looking at it now, it might be better to return a table with status and content in both cases and just have the content vary?

face-hh commented 4 months ago

Should this comply with the sandbox change in #121?

Quoting the code change:

-    let ok = lua.load(luacode).eval::<LuaMultiValue>();
+    if let Err(e) = lua.sandbox(true) {
+        lualog!("error", format!("failed to enable sandbox: {}", e));
+        Err(LuaError::runtime("failed to enable sandbox"))
+    } else {
+        let ok = lua.load(luacode).eval::<LuaMultiValue>();
+    }

Also, os.time() has been added by #121 so there's no need for a dedicated time function.

nullsoepic commented 4 months ago

Considering #121 has been merged, It is probably a good idea to do so, will update asap. Still need an opinion on the changes to fetch and suggestion from the previous comment

face-hh commented 4 months ago

it might be better to return a table with status and content in both cases and just have the content vary

I agree with this but it will break current websites, I propose to revert the fetch behavior to the old one

nullsoepic commented 4 months ago

Alright fetch will be reverted. Just to note, require still has to be given a http(s):// url as I could not get the taburl in the context of the function (any suggestions are appreciated). No ideas on how that could be properly implemented atm, will most likely remain as is for now.

face-hh commented 4 months ago

It doesn't seem like you've reverted the fetch implementation as I'm getting this error (which doesn't occur on the master branch):

runtime error: [string "src\b9\lua.rs:445:22"]:57: attempt to concatenate string with nil

I've tested the JSON utility and it seems to work, I'm gonna check out the require feature in a bit

face-hh commented 4 months ago

Alright, everything seems to work

hexxt-git commented 4 months ago

epic