How do exceptions during Rhino readFile work? - rhino

I have this code running inside Rhino under Linux. The file doesn't exist.
try {
var u = readFile("/tmp/wtf");
print(u);
} catch (e) {
print("error!");
}
The code in the 'catch' doesn't run, even though the file definitely isn't there. I just get a blank value assigned to 'u'. Is this normal?
Are there other situations (besides a file missing) where the catch would run?
Could I differentiate an empty file from a missing one without invoking some other function? (I realize Rhino gives me access to most of the standard Java libaries).
Just want to confirm that readFile is always synchronous?
I can't find anything much on SO or MDN about how readFile works. Any insight appreciated.

Related

is it possible to call require from C

I have a module compiled in a shared object (I followed the library part of this article https://chsasank.github.io/lua-c-wrapping.html) and I want to load it from C not from the interpreter.
Is it possible ? If so how to do it ?
Yes, it's possible, as require is a function stored in a global environment. Lua does the same in standalone interpreter when it needs to process the -l option, see the dolibrary function.
You do this the same way as with any other global function - in simplest case calling lua_getglobal(), then pushing the name of the file to require, and calling lua_call/lua_pcall/whatever.
I know that I am late, but someone else may struggle with this right now (like I just did).
This is a simple way of doing "require" from C:
int reqRes = luaL_dostring(L, "local t=require('myLib') return (t~=nil)");
if (reqRes==0)
//success
else
//failed
Unfortunately, right now, I'm using Lua 5.1 and "dolibrary" function doesn't exist, I >tried to take some part of the code and it crashes :\ So, for now, I use luaL_dostring(L, >"require 'libMyWrappings'"); libMyWrappings must be in the same directory as the c >program, and I can't use a path to indicate the lib. – Aminos Jan 22 at 11:45
I just ran into the same issue, it has to do when the package library is loaded
{LUA_LOADLIBNAME, luaopen_package}
needs to happen before you try and call it

Download repository from Github

I'm trying to learn Yeoman, but find the official documentation severely lacking. I've found the remote() function which appears to download a GIT repository but whatever I do I can't get it to work without throwing errors.
Here's what I have:
this.remote('powerbuoy', 'SleekWP', 'master', function (err, remote) {
if (err) {
this.log(err);
return err;
}
remote.copy('.', this.destinationPath('wp-content/themes/sleek/'));
}.bind(this));
What I'm hoping would happen here is that the https://github.com/powerbuoy/SleekWP/ repo is downloaded and moved to wp-content/themes/sleek/. What happens instead is I get:
fs.js:603
var r = binding.read(fd, buffer, offset, length, position);
^
Error: EISDIR: illegal operation on a directory, read
Is there a better documentation or a tutorial explaining all these basics somewhere? I'd love to know how to copy files without each copy being printed to the console too for example. This all seems pretty basic but http://yeoman.io/authoring/ is very sparse.
Ok, so apparently the solution was to use remote.bulkDirectory() instead of remote.copy().
Edit: However, reading the "documentation" (can barely be called that) it says that "You should never use this method, unless there's no other solution." (http://yeoman.io/generator/actions_actions.html)
So if anyone knows of the proper way to do this I'd love to know.
I switched to the fs-extra package and used cacheRoot() and destinationRoot() to copy the directory instead:
fs.copy(this.cacheRoot() + '/username/Project/branch/', this.destinationPath('destination/path/')

Version 2.0 script causes exception when run under version 3.0

WRT this question -- I got something working by getting the current 'prompt' function as a string and writing a new .ps1 file defining 'prompt'.
In writing the new function, I put the functionality I want to add in a try/catch block, and write the existing prompt functionality into a finally block.
Not at all ideal, but it works in v2.0. When I run it in v3.0, I get an exception:
"Control cannot leave a finally block"
Is there any way to for a script to ask the host for a certain version's behavior?
Thanks for any insights.
Current 'prompt' function consists of:
return " > "
I want to add some functionality to the prompt function, so I write a new temp.ps1 file. I add my functionality in try/catch block, and include the existing in a finally block.
The temp.ps1 looks something like:
function global:prompt {
try {
pushd
}
catch {
$errors[0] | fl * -force
}
finally {
return " > "
}
}
This works as expected in v2, causes the "Control cannot leave..." error in V3.
If you plan to keep using Powershell V3, you cannot have a return statement in your finally block. See Page 6 of Windows Management Framework 3.0 Release notes: (You shouldn't need a return value there anyways)
http://download.microsoft.com/download/5/2/B/52B59966-3009-4F39-A99E-3732717BBE2A/WMF3%200%20Beta%20Release%20Notes.docx

Evaluate large Groovy script in in GroovyShell

I'm using GroovyConsole to evaluate scripts I get from external sources. So the code to evaluate is dynamic and I don't have control over it. Actually is written into a database and I have to read it as a String. Not perfect, but that's how it is.
What I'm doing right now:
private GroovyShell shell
def processScript( def script){
if (script) {
try{
shell.evaluate (script, 'some_random_name')
}catch( e ){
log.warn "Could not process script: $e"
}
}
}
This usually works. But now we got a large script (~3000 LOC) and it throws java.lang.RuntimeException: Method code too large! because the script is larger than 64K.
I tried to dump the script into a file and use a BufferedReader, but it throws the same Exception.
So is there a better way to evaluate dynamic Groovy code from within a Groovy method?
The problem is your script reach the java limit for a method. I think the only solution is to split your script in many scripts in some way.
See this answer

Run script instructions from external source in Lua

Ok, I'm wanting to know if there's a way of running scripts from an external source with Lua. Be it another file in .txt format or from pastebin, what have you, and run the code and wait for said code to finish, and then continue on with the rest of the function. I'm not quite sure at all about how it'd work, but this is basically the idea I'm going by and isn't actual code.
function runStuff()
print("checking for stuff")
run.script(derp.txt)
wait for script
if run.script == finished
continue program
elseif nil
print("looks like this script sucks.")
end
end
runStuff()
And for example what "derp.txt" contains is:
function lookFile()
if herp.txt exists then
rename herp.txt to herp.lua
else
nil
end
end
lookFile()
I'm still new to Lua so I'm coding like a moron here, but you get my picture hopefully. I'm working on a project that'll act like an installer package for repositories based from pastebin or anywhere else that'll supply raw format outputs of lua scripts and I'm going to use that idea for it to call on scripts to run externally. So when I supply a "First Time Run" version of the program, it'll call out to a lua script that'll call to another lua script and install that script, then close.
This is for minecraft, mind you. ComputerCraft made me take interest in Lua, but anyway, hopefully you got the gist of what I'm trying to figure out. Hopefully that's doable and if not, I'll just have to figure something else out.
To load and execute a Lua code fragment you can use something like this:
local http = require("socket.http")
local response, err = http.request("url to some Lua code")
if not response then error(err) end
local f, err = (loadstring or load)(response)
if not f then error(err) end
print("done with "..response)
-- make sure you read on (in)security implications of running remote code
-- f() -- call the function based on the remote code
You probably need to use sandboxing.

Resources