Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Promise Feature #206

Open
MarketingPip opened this issue Nov 15, 2022 · 13 comments
Open

Feature Request: Promise Feature #206

MarketingPip opened this issue Nov 15, 2022 · 13 comments

Comments

@MarketingPip
Copy link

It would be nice to have a promise based feature to execute code via the JavaScript API.

Maybe - I missed this in documentation (but I am searching deeply & don't see anything).

An await function would be more than ideal that either throws an error so you can catch it or etc...

@daurnimator
Copy link
Member

The lua API is synchronous. Why/when would you have a promise?

@MarketingPip
Copy link
Author

The lua API is synchronous. Why/when would you have a promise?

My apologizes- late night last night lol.

In that case - how does one execute a function via the JS API so that if an error occurs in Lua - a nim value is returned. You can console log it / use it in the HTML document etc.

When I tried executing a false function via JS API such as something random like "bshfhfjgh". It throws an error in console log. Tho I do not know how to handle the error message / return the Lua error message in preferably in Pure JS (not in Lua such as the REPL example on homepage).

ps; thanks for quick reply @daurnimator 👌

@catwell
Copy link

catwell commented Nov 15, 2022

@MarketingPip I think what you want is this

@MarketingPip
Copy link
Author

@MarketingPip I think what you want is this

@catwell thank you 👌

@MarketingPip
Copy link
Author

@catwell - can you give me a demo of usage please... sorry to be a noob but I can't seem to catch the errors properly still.

@MarketingPip MarketingPip reopened this Nov 15, 2022
@daurnimator
Copy link
Member

sorry to be a noob but I can't seem to catch the errors properly still.

See the code that @catwell linked:

	let status = lua_pcall(L, nargs, 1, 0);
	let r = tojs(L, -1);
	lua_pop(L, 1);
	switch(status) {
		case LUA_OK:
			return r;
		default:
			throw r;
	}

To use the fengari API, you call lua_pcall, then look at the return code to know if it succeeded/had an error/etc.


a nim value is returned

What is "a nim value"?


Or is your whole question about how to use fengari-interop?

@MarketingPip
Copy link
Author

@daurnimator sorry to be a complete noob again. 😞 I'm trying my best to find the documentation / repo etc I need to do this properly. But I'm thinking I should ask directly here. Plus I would assume you might want to possibly document this / make it stick out more in the documentation.

I am trying to evaluate Lua commands from a user input in the browser (like the demo on homepage - but NOT in Lua.) Can you please provide the the import statements / usage on how to do this. As I can't seem to figure this out myself.

Just to clarify -

I know I'll need the feature / example usage to properly change error messages from being displayed in console etc.

&

example of calling p_call with usage of a import module in the web browser. A example with import from CDN link would be great.

Off topic as well - I was hoping to ask this. Is the CSS for the terminal on the home page taken from somewhere etc..? Or is it custom made? And am I allowed to use it commercially or non commercially?

Thanks in advanced 👌

@daurnimator
Copy link
Member

I know I'll need the feature / example usage to properly change error messages from being displayed in console etc.

error messages aren't shown in the console unless you put them there. (which e.g. fengari-web does for <script> tags over here: https://github.com/fengari-lua/fengari-web/blob/77d35f5e1516f431cbb96e165b4272f2fc9b0b3a/src/fengari-web.js#L181-L183 )

@MarketingPip
Copy link
Author

@daurnimator - my apologizes I said that at the start! I didn't know what repo I should be posting under :(

Should I redirect my question / issue there? And if not - can I ask for example of usage via Fengari-Web as a import (not running code inside of a script tag) - with disabling console log errors.

I am looking looking to achieve something like the following -

import {fengari-web} from "CDN-URL"

function runLuaCommand(command){
try {
    // display Fengari Web output to DOM 
  document.body.innerHTML =jscall(command)
} catch (error) {
  // display Fengari Web errors to DOM 
  document.body.innerHTML = error;
}

Which I do understand know I need to check if NIL value is included (not use a try / catch statement) - but again still confused on what library I need to use / the proper command to call and how to disable console log messages etc...

Sorry again to ask you to literally spoon feed this to me - but I am struggling trying to find the proper usage via Fengari to do this. (hence I figured this was best repo to post in! not knowing if I require web version or etc...).

ps; I did try searching other issues / comments you have posted in 2019 etc but still no luck 😢 lol

@daurnimator
Copy link
Member

@MarketingPip perhaps have a look at how we do it on the fengari website itself:
https://github.com/fengari-lua/fengari.io/blob/52035296f8ee21eb88b923f5f698c1fb86d7cda2/static/lua/web-cli.lua#L46

==> we replace the print function with one that appends to the output buffer (+ other useful things like make sure it's scrolled to the bottom).

@MarketingPip
Copy link
Author

@daurnimator - yeah I seen the website version (hence I also asked about the CSS licensing used / if it's a public library etc as well!)

But didn't know if there was a way to write it in purely JS to call the Lua Compiler - without allowing access to the DOM. < which I guess would be my feature request if again - there is no way to directly do this already.

@daurnimator
Copy link
Member

But didn't know if there was a way to write it in purely JS to call the Lua Compiler - without allowing access to the DOM.

There is. That is the lower level on top of which everything else is built. It's the standard Lua C API. And then you have fengari-interop to help you marshall values to/from. fengari-web is to make it so you can write Lua in the browser just like you can write Javascript.


You're simultaneously asking for low level features and high level features, and it doesn't quite make sense.
Generally for any interactive Lua intepreter, to get output you're likely just going to want to replace the print function.
You can write that print function in javascript (either low level or high level via fengari-interop), or in Lua itself.

@MarketingPip
Copy link
Author

@daurnimator I am sorry to be a noob but can I please get a demo / sample of what I need. I did figure out how to execute code in a try / catch etc. But I do not get Nil returned - and only other error messages in (stack, message).

As well the online demo - the same random commands "just some random letters" typed would return sometimes a Nil message other times the error .message key (I do believe)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants