How to compile Javascript console application locally? - javascript

I am a beginner in Javascript, I decided to practice Javascript by problem solving using it, I found an online judge that accepts Javascript V8 4.8.0 code.
So, I searched online to get that version of Javascript V8 on my machine, but I couldn't find any easy way, All the pages were explaining how to build it, and it seems to be a process that I don't need to go through.
Is there an easy way to compile and run command line apps written in Javascript on my machine?
Note: I don't want to use node.js because I tried using it's I/O and
as a beginner I think it is complex in some way.
Update: I found that package manager pbox.me which provides a version of V8 JavaScript Engine and I managed to install it.
Yet another problem appeared: whenever I try to run a js file writing d8 myfile.js in command line nothing happens as if it is an empty program, knowing that I tryied to d8.exe file and it is working, and I made sure the PATH is inserted in the environment variables.
What am I doing wrong?

The easiest way to get started with JavaScript is probably to use it in a browser. You can type simple things directly into the browser's JavaScript console (check the menu); or you can embed your code in a simple HTML document.
If you want, you can even pretty easily implement the readline()/print() functions, so you can pretend to be doing stdin/stdout based I/O: just read from an array of strings, and send output to console.log (or create DOM nodes if you want to be fancy and/or learn how to generate dynamic website content by hand).
Side note: V8 4.8 is severely outdated, don't use it to execute code you haven't written yourself.

Related

How can I get a JavaScript v8 console for offline usage?

I am practicing online programming contest held by codeforces, and I try to play javascript with it. However, I find it very difficult to setup my environment for offline test, they have readline() for input to stdio and print()/write() for output in stdout. Any help? Thanks.
For practising Javascript, you could use any thing from the chrome development console to online editors like JSFiddle. Nodejs allows you to run Javascript (which is used for client side rendering) on a server. What this means is that using node, you could run javascript on your terminal. As for the input , output functions you mentioned, it is easy to require them as node modules or use appropriate Javascript libraries for the same.

How to run JavaScript/AppleScript from cocoa app?

I am trying to execute some AppleScript from Objective-C using NSAppleScript... however, the code I am trying is the new JavaScript for automation in Yosemite. It does not appear to do anything when it is run, however, normal AppleScript works fine.
[NSApp activateIgnoringOtherApps:YES];
NSAppleScript *scriptObject = [[NSAppleScript alloc] initWithSource:
#"\n\
iCal = Application(\"Calendar\");\n\
iCal.includeStandardAdditions = true;\n\
iCal.activate();\n\
iCal.displayAlert(\"testing\");\n\
"];
[scriptObject executeAndReturnError: nil];
How can I get this to run?
Thanks
NSAppleScript is hardwired to compile source code as AppleScript. You'd need to use OSAKit instead, which is shoddy and undocumented, but at least allows you to specify which language to use when compiling from source. Alternatively, you could kludge a workaround by piping your source to osacompile -l JavaScript, then loading the resulting compiled .scpt file into NSAppleScript.
However, it's not clear from your example why you're using NSAppleScript. If you want to execute user-supplied scripts you should probably look at NSUserAppleScriptTask: it's even crappier than NSAppleScript, but it's designed to run the script in a subprocess outside your app's sandbox. (NSAppleScript runs scripts in-process, which in a sandboxed app prevents user scripts talking to arbitrary applications.)
OTOH, if you're only using NSAppleScript to run your own code, you'd be much better using Scripting Bridge (which is crappy and broken, but may be sufficient if your needs are modest), or use AppleScript via the AppleScript-ObjC bridge, which allows your ObjC code to use AppleScript-based 'classes' much as if they were native Cocoa classes. Given that AppleScript is the only supported solution that knows how to speak Apple event correctly (JXA's riddled with flaws too), I'd recommend the latter.

Javascript code

I checked some websites source code and JavaScript games. The problem is that everything is readable and understandable except for JavaScript code that is isolated on a file with the extension .js. It looks like this:
{Vargas=void0,h=!0,Ge=null,l=!1,AA=component,BA=Infinity,ca=set Timeout,DA=is Nan,m=Math,ea=deconstructionism;function He(a,b){return a.on-load=b}function IE(a,b){return a.on error=b}function ha(a,b) {return a.name=b}
As you can see, it's hard to read this code because of the stupid indentation. I tried to use Microsoft visual web developer and free JavaScript editors to organize the code, but it was all useless!
How can I make it more readable?
The best place to start is to look at other open source java script libraries/modules/plugins. You must have the original code though, because what you see in the browser is already "compiled" for the web to be small and fast.
For the client you have plenty frameworks. Look for example at the list jsfiddle uses (top-left). You can also use this tool to play with javascript without having to install anything. Search on the web for those projects (that jsfiddle uses as libraries) and look into the code.
There is also a server-side javascript library that allows you to write javascript code also for the server (also web apps, the server side of them). This is called Node.js Ceck this page to find out more. In Node.js you have almost an infinite number of open source and small modules: see the node module registry wehre you find the links also to the individual project.
In any case, you certainly need a Github account to play with other's code because most of these projects are stored on Github.

Starting a process via Javascript [Using Rhino JS]

I'm working with a tool that only allows for Javascript as the scriptting language. With the script, I need to launch a process. How would I go about this?
The javascript code is running on the client that will launch the process. The javascript interpeter is RhinoJS.
So my question remains:
1. Is there a way that I can call a specific Java class from Rhino [ProcessBuilder]?
or
2. Is there a way to launch an executable from Javascript? [I've tried the UniversalXPConnect route, but it turns out that the version of Rhino I'm using doesn't really worry about permissions]
That was quick [I found the answer right after I asked]:
var pb = new java.lang.ProcessBuilder("notepad.exe", "c:\test");
pb.start();
Basically RhinoJS has a quirk to allow it to directly access Java functionality. So basically once should just launch the process from there.

file layout and setuptools configuration for the python bit of a multi-language library

So we're writing a full-text search framework MongoDb. MongoDB is pretty much javascript-native, so we wrote the javascript library first, and it works.
Now I'm trying to write a python framework for it, which will be partially in python, but partially use those same stored javascript functions - the javascript functions are an intrinsic part of the library. On the other hand, the javascript framework does not depend on python. since they are pretty intertwined it seems like it's worthwhile keeping them in the same repository.
I'm trying to work out a way of structuring the whole project to give the javascript and python frameworks equal status (maybe a ruby driver or whatever in the future?), but still allow the python library to install nicely.
Currently it looks like this: (simplified a little)
javascript/jstest/test1.js
javascript/mongo-fulltext/search.js
javascript/mongo-fulltext/util.js
python/docs/indext.rst
python/tests/search_test.py
python/tests/__init__.py
python/mongofulltextsearch/__init__.py
python/mongofulltextsearch/mongo_search.py
python/mongofulltextsearch/util.py
python/setup.py
I've skipped out a few files for simplicity, but you get the general idea; it' a pretty much standard python project... except that it depends critcally ona whole bunch of javascript which is stored in a sibling directory tree.
What's the preferred setup for dealing with this kind of thing when it comes to setuptools? I can work out how to use package_data etc to install data files that live inside my python project as per the setuptools docs.
The problem is if i want to use setuptools to install stuff, including the javascript files from outside the python code tree, and then also access them in a consistent way when I'm developing the python code and when it is easy_installed to someone's site.
Is that supported behaviour for setuptools? Should i be using paver or distutils2 or Distribute or something? (basic distutils is not an option; the whole reason I'm doing this is to enable requirements tracking) How should i be reading the contents of those files into python scripts?
The short answer is that none of the Python distribution tools is going to do what you want, the exact way you want it. Even if you use distutils' data_files feature, you're still going to have to have your javascript files copied into your Python project directory (i.e., somewhere under the same directory as your setup.py.)
Given that, you might as well just copy the .js files to your package (i.e. alongside mongofulltextsearch/init.py) as part of your build process, and use package_data or include_package_data=True.
(Or alternatively, you could possibly use symlinks, externals, or some such, if your revision control system supports those. I believe that when building source distributions, the Python distribution tools convert symlinks to real files. At least, you could give that a try.)

Categories

Resources