How compile function written in python to JavaScript (emscripten)? - javascript

I have a simple function written in python which I want to port to javascript.
I have compiled python 2.7 into a .so library, so thats not the issue.
The problem I'm having is that after I compile my program with cython, the function names get all scrambled up, which means I don't know how to preserve the functions when i run emcc.
Does anybody have any experience compiling python programs to js with emscripten?
Any information would be appreciated.
Note: I want to preserve the exact functionality to that of python, I don't want something that translates a python program into javascript.

This other question, with an accepted answer, complains about the same issue: Cython mangling function names and making it difficult to access from C++: Embed python function in C++
The accepted answer states that Cython isn't meant for this sort of thing at all, suggesting you can't do what you want in this fashion:
You're not going to be able to get the interoperation you want that way. If you open and inspect hello.c you won't find "static int say_hello" anywhere in there. Cython is designed for letting Python use C libraries, not letting C libraries use python.
The not-accepted next answer suggest that specifying public will not mangle the function name, although he also mentions linking problems.
# (in the generated C file hello.c)
__PYX_EXTERN_C DL_IMPORT(...) say_hello(...);
Worth a shot, but please consider the other options in the comments if it fails.

Related

RedBaron analog in javascript?

I need to read a python file from javascript. I used in another project RedBaron which was quite helpful since I can easily access different node types and their value. Since I cannot use this library in Javascript since RedBaron is a python library I wanted to ask you all if you know a library that is similar to RedBaron.
Thanks in advance!
I tried using a line by line reader in Javascript but it is not sufficient for my needs. As I explained earlier I want to access different node types their values or operators.

is there a way to write a Java method like toString in Javascript which if called on a method prints the source code of the method?

Sorry if this is a trivial question, so if this has already been asked, please direct me to the question.
I know that the tostring method in javascript, if called on a function will print the source code (more about it: link). Is it possible to do the same thing in Java?
So if I have the following identity function definition:
public class class1 {
int f1(int x){
return x;
}
}
And the following main function:
class Main {
public static void main(String args[]) {
class1 c1 = new class1();
????
}
}
Is there anything I can put in place of '????' that would print something like
int f1(int x){
return x;
}
Disclaimer: I'm not an expert at Java, or any programming language for that matter. However, I do know how to find information online.
This concept does not seem very doable within Java. To start:
JavaScript is an interpreted language, not a compiled language. A program such as C++ or Java needs to be compiled before it is run. The source code is passed through a program called a compiler, which translates it into bytecode that the machine understands and can execute. In contrast, JavaScript has no compilation step. Instead, an interpreter in the browser reads over the JavaScript code, interprets each line, and runs it. More modern browsers use a technology known as Just-In-Time (JIT) compilation, which compiles JavaScript to executable bytecode just as it is about to run.
Basically, JavaScript has the advantage of reading directly from the file with the source code, and executing it on the fly. Compiled languages such as Java won't have that sort of functionality built in by default for many reasons, including security. An application should be able to run without allowing hackers access to its source code as much as possible.
There have been attempts at doing various forms of what you're interested in, but the two easiest methods seem to be
Printing the original .java file line by line
Storing a string reference to the entire code or the method(s) required.
It also seems possible to print the method name, but not the body.
Aside from that, the only thing you might be able to get from a compiled, running java program, is bytecode, which would require a decompiler to have any hope of understanding the source behind it.
You can continue reading up on it through a few of the links here:
How do I print the method body reflectively?
Save a method into a variable, java 8
View a method's implementation (method body) using Java Reflection
Probably yes, but not a trivial one with a ready command. JavaScript is an interpreted language where the executing environment has access to the source code in its original form. That is how you can inspect it in a browser console and see the same variable names as are in the source code.
While the compiled/interpreted distinction is fuzzy for Java, it is definitely modified before execution. The bytecode used by Java's Just-in-Time compilation may be more readable than a fully compiled binary file, it is not the source. If the running program does not have access to the source code, it is less able to output it. A debugger running in an IDE can reference problems in the source; otherwise, you are limited to debugging the bytecodes.
This is why Keno Clayton suggested the question on Quine programs, which are meant to reproduce themselves. This answer outputs the source code by hard-coding it as a class attribute. You could take a similar approach with a pre-compilation script that went through all the methods and made strings out of their sources, but the result would be bulky and potentially sensitive.

Call certain Python function from JavaScript

I've searched this subject already for a bit on the Internet and I couldn't find anything decent:
I was wondering if it possible to call a single Python function from a Python file from JavaScript?
I already came across suggestions such as $ajax-requests, (Call Python function from Javascript code) but this only seems to execute the __main__ method of said Python file. Other suggestions I came across also just execute the entire Python file, without the possibility of just executing a single method of it.
What I want is for example a Python-class containing methods this:
def testMethod(self):
print "testMethod called from JavaScript"
return "It worked!"
and that I would be able to call this testMethod() from JavaScript (optionally with parameters etc. of course if the method expects them).
Is this possible in a simple way? I mention the word simple because frameworks like Flask etc. would be my last resort, I'd much more prefer something fast and doesn't change my webproject too much for the rest.

How do I add my own JavaScript libs to ClojureScript?

I want to write a Google Chrome extension, using ClojureScript. With ClojureScript I can use all the Google Closure libs, but afaik access to the Chrome browser is not included in those libs. So I want to wrap all the Chrome stuff in my own JavaScript lib.
So far I tried creating my own jar that has a single JavaScript file that just creates a Foo object and exports the constructor. I'v added this jar to the lib directory of the ClojureScript compiler (which also has for example goog.jar), but so far without luck:
Exception in thread "main" java.lang.IllegalArgumentException: No implementation of method: :-compile of protocol: #'cljs.closure/Compilable found for class: nil
at clojure.core$_cache_protocol_fn.invoke(core_deftype.clj:494)
at cljs.closure$eval1056$fn__1057$G__1047__1064.invoke(closure.clj:187)
at cljs.closure$get_compiled_cljs.invoke(closure.clj:422)
at cljs.closure$cljs_dependencies.invoke(closure.clj:440)
at cljs.closure$add_dependencies.doInvoke(closure.clj:462)
at clojure.lang.RestFn.applyTo(RestFn.java:139)
at clojure.core$apply.invoke(core.clj:602)
at cljs.closure$build.invoke(closure.clj:701)
at user$eval1246.invoke(cljsc.clj:21)
at clojure.lang.Compiler.eval(Compiler.java:6406)
at clojure.lang.Compiler.load(Compiler.java:6843)
at clojure.lang.Compiler.loadFile(Compiler.java:6804)
at clojure.main$load_script.invoke(main.clj:282)
at clojure.main$script_opt.invoke(main.clj:342)
at clojure.main$main.doInvoke(main.clj:426)
at clojure.lang.RestFn.invoke(RestFn.java:421)
at clojure.lang.Var.invoke(Var.java:405)
at clojure.lang.AFn.applyToHelper(AFn.java:163)
at clojure.lang.Var.applyTo(Var.java:518)
at clojure.main.main(main.java:37)
Has anyone tried this before?
Take a look at this post from Luke Vanderhart: "Using JavaScript libraries in ClojureScript"
http://lukevanderhart.com/2011/09/30/using-javascript-and-clojurescript.html
Also, this video from Kevin Lynagh: "Extending JavaScript Libraries from ClojureScript"
http://www.youtube.com/watch?v=XfzXFWTT-z0
If I remember Rich Hickeys talk correctly the whole program optimization of the closure compiler needs any library code to adhere to certain coding conventions. I think he also said something of JQuery not doing this but Dojo doing this. I never did this but you might find some useful information here

JavaScript code completition done right?

I've tried some of the editors/IDEs regularly recommended for coding JavaScript (Aptana, WebStorm, ...) but none of them has a satisfying autocomplete functionality. I'm probably spoiled by Microsoft's IntelliSense for .NET. There is some JavaScript-IntelliSense in WebDeveloper, but that seems to be a stripped-down version. The best I've found so far is WebStorm, but its code completition is easily distracted by imported libraries (offering hundreds of suggestions) and identical function names.
Did I miss an editor/IDE that uses refactoring (or something else) to offer proper code completition, so that it really "knowns" what that variable-name stands for, I just put a dot behind? Or is something like this on its way?
I always recommend Komodo Edit from ActiveState (now up to version 6, with support for HTML 5 and CSS3 as well as recent versions of Javascript, PHP, etc.) Note that you may have to install addons for the languages you're working in, but you should find them through the Mozilla-like Addon manager.
Also supports jQuery and even lets you use jQuery (along with vanilla Javascript or Python) in its powerful macro IDE.
Code completion example:
<script type="application/x-javascript">
var obj = {};
obj.personnel = [{firstName:"John", lastName:"Brick", age:43},
{firstName:"Jane", lastName:"Motte", age:26}
];
// now type obj. and code completion immediately offers you "personnel"
// note: file must be saved for the app to find all members of declared
// variables, but I save about every 10 seconds so it's not a problem
</script>
The best I've found so far is
WebStorm, but its code completition is
easily distracted by imported
libraries (offering hundreds of
suggestions) and identical function
names.
This comment confuses me. If you import the libraries, and your code is using them, why is it bad to include the function names in the code completion suggestions? Wouldn't you want to have jQuery's functions included if you're using it?
If you're using Microsoft's IntelliSense with jQuery, does it stick to its guns and only show JavaScript core functions? Sounds limited to me, unable to be smart when I add libraries.
Or is something like this on it's [sic] way?
It sounds to me like you want a clairvoyant interface. I don't think it is on the way anytime soon.
By the way, "it's" == "it is"; "its" is the possessive.

Categories

Resources