I'm building an app for iPhone which will hopefully use the new JavaScriptCore framework added to iOS 7. I am using javascript as I need to add a javascript library citeproc-js to my project. I can get this library up and running by loading it in a UIWebView but I need to use it from a background thread so am trying to load it in a JSContext instead.
The problem I'm having is the library relies on using the DOMParser of webkit but the parser never loads when the framework is loaded into a JSContext.
Should it be possible to access a DOMParser from a JSContext? If not is there an alternative?
Thanks!
The JavaScript API provided by JSContext is actually the core API of the language - it even lacks stuff like console.log and setTimeout, because those are provided by browsers. Because DOMParser is another API defined outside of JavaScriptCore it shouldn't be accessible in JSContext.
What you can do is expose a native XML parser like TBXML to the JSContext via the JSExport mechanism and consume that instead.
In case it simplifies things for readers hitting this question, the citeproc-js implementation of CSL now contains a string-based XML parser written in pure JavaScript. No exceptional setup is required beyond the essentials: if the processor is fed a valid XML string, it will work with it.
(Posting as the author and maintainer of citeproc-js)
Related
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.
I am attempting to migrate an ASP Classic application to .Net 4.0 (4.5 would also be acceptable). The application is responsible for performing XSL Transformations using Javascript extension functions. The ASP Classic application currently uses MSXML 4.0 to perform the transformations.
I am running into problems in .Net when I try and use the XslCompiledTransform class to process a transform that includes javascript extensions. It works for simple examples but we have a fairly extensive javascript library and I am seeing error as the test cases become increasingly complex. The most frustrating of these errors are syntax errors, for example the .Net parser doesn't seem to like statements outside of functions.
Can anyone tell me which class is used to parse and compile the javascript extensions and if there is thorough documentation anywhere. Also can the the javascript processor object be replaced with a custom object (in a similar manner to a UriResolver).
Also, is it possible to use the parser rules to have Visual Studio highlight javascript syntax errors when viewing the file. I am pretty sure it already does this for javascript code that is used in web pages and such, but the syntax rules seem to be different in XSLT.
AFAIR XslCompiledTransform uses CodeDom to compile embedded scripts. The way it works is that XslCompiledTransform creates a code file that is then passed to CodeDom for compilation. I believe MSXML uses a different JavaScript engine than the one used by CodeDom. Hence the compilation problem. In addition (it's been a while since I looked at it) there may be some additional processing happening to make the scripts compilable (I remember that for scripts in C# there was a class created and all functions were put to this class).
Note that the assemblies created by CodeDom are loaded to the appdomain where the transformation is executed. This may lead to OutOfMemory exceptions since the CLR does not allow to unload assemblies - I wrote about this some time ago here: http://blogs.msdn.com/b/xmlteam/archive/2011/09/26/effective-xml-part-5-something-went-really-wrong-outofmemoryexception-and-stackoverflowexception-thrown-when-using-xslcompiledtransform.aspx
I have found a useful billards physics library in Borland delphi for my hobby project. The code is object oriented(in Object pascal). I want to visualize it using webGL (javascript) in chrome browser.
The idea is, I should have one call to make from the javascript which will access the interface from delphi lib and give me the new position, rotation and scale values in respective array.
For this I want to develop an NPAPI lib Scriptable plugin in Delphi(Meaning NO UI only lib of APIs).
1)I want to export those necessary physics APIs from the Delphi via this NPAPI plugin and access them in my html page using JavaScript. I am targeting chrome.
2) Any simple "hello world" tutorial or article related to this will be highly helpful.
3)Any help on using Firebreath to achieve the same is most preferable.
How to use Delphi with Firebreath?
use a good HTML / JavaScript editor (for example the NetBeans IDE) create a basic HTML page which contains the "hard-coded" JavaScript to display the initial scene, and a HTML form to accept user input
use Indy or Synapse to create a HTTP server (in Delphi), wich serves this HTML page and when the user submits form input, parse the form parameters and pass them to the billiard simulation, which should emit a list of graphic commands as a result - and convert these to WebGL (this is the hard part) statements
from the HTTP server, send a response (HTML document) back to the client, with the JavaScript / WebGL statements embedded, so that the client will see the animation and the new playfield state
repeat until end of game
Create a COM DLL and install it on the client's machine. Then Javascript can use its ActiveXObject class to access the COM object (or, under IE, you can use the <object> HTML tag). Make sure your COM object implements the IDispatch interface, as Javascript needs to use late binding since it is an interpretted environment and not compiled.
I am attempting to create an html document parser with Python. I am very familiar with jQuery and I would like to use its traversing functionality to parse these html files and return the data gathered with jQuery back to my Python program.
Is there any way to use javascript scripts through Python? Or is this just a pipe dream?
You might not need to do this. There is a Python module called PyQuery that directly emulates the API for jQuery. It works exactly as you would expect it to in almost every way. Give it a shot!
jQuery itself does not contain an HTML/XML parser at all. It uses the browser to do all its parsing. Thus, even if you figure out how to run Javascript from Python, it won't do you any good.
jQuery doesn't parse HTML - it traverses the DOM. You'd need an entire rendering engine (e.g. WebKit) if you wanted to use jQuery to work on the HTML.
Well from your question it seems you will require python-javascript bridge like
Pyjamas http://pyjs.org/ , PyPy http://codespeak.net/pypy/dist/pypy/doc/ , skulpt http://www.skulpt.org/ . Or my personal favorite PyXPCOM http://pyxpcomext.mozdev.org/ it installs a python backend directly into the firefox browser and using xpi stubs one can make bidirectioal calls ( mind you very complicated )
I really like the idea of automatic binding generation like SWIG does. But it is still lacking Javascript binding. I read that it could not be done with Spidermonkey because of the JS Context that must be passed as parameter to each function.
The only solution I found is to embed a JVM into my C++ application, generating bindings to Java using SWIG, and then using Rhino engine as the JS interpreter. It works fine, but it is really heavyweight!
Any other ideas?
Note: yes I know, I could use Python or Lua instead. But my question is really about JS.
If you are not in a hurry: Wait for the SWIG Javascript bindings that might be developed as GSoC-2012 project "New module for Javascript" by Neha Narang.
The abstract says:
This project aims to add Javascript as a new supported target language
to swig to automate the generation of Javascript wrappers over C++.
The project repository is https://github.com/Neha03/gsoc2012-javascript.
Well, since I don't have any answers, I think that I must use other alternatives than SWIG.
I found that page on the mozilla developer center mentioning several projects for wrapping JS with the Spidermonkey engine: Spiderape, Flusspferd, TriXUL and jsapigen.
For V8, there is a similar project called v8-juice from the creator of Spiderape.