Call binary from React Native? - javascript

Is it possible to include and exec a compiled binary inside a React Native project? I'm exploring handling the business logic in React Native with languages other than Javascript, but still have the compiled code be cross platform.

You can access native modules, so uncompiled source code in either Java or Objective-c / Swift is possible. Compiled binaries are always compiled for one platform, which means that you can not link such binaries in to react native. For moving the logic away from Javascript, you have two options
Connect a web server and store most of the business logic there, so use the iOS / Android app mostly as a view layer.
Find a transpiler, that transpiles the language you would like to use to Javascript

Related

How to create a node.js Native Image with GraalVM

I am trying to create a native image from a simple node.js example.js application.
When running the application with:
node --native -i --native example.js
then the application is starting and working as expected.
Now I would like to create a native image. I tried the following command:
native-image --language:js example.js
however it is not working, because of the error:
Build on Server(pid: 77626, port: 64052)
[example.js:77626] classlist: 3,964.04 ms
error: Main entry point class 'example.js' not found.
Error: Processing image build request failed
As a resolution I created a main entry point in example.js such as:
function main(args) {
console.log("Main app started")
}
however this doesn't work.
Is there a way usually native images are create for js/node.js applications?
No. Currently, as of Dec 2018, creating GraalVM native images for node.js applications is not possible. The native image utility takes Java bytecode and compiles it ahead of time. GraalVM JavaScript engine is a Java program and can be compiled as a native image. This is what you actually run when you execute $GRAALVM_HOME/bin/js. It still loads JavaScript at runtime, interprets it and compiles it just-in-time to machine code.
GraalVM's node implementation is a normal node, a native application, with the JavaScript engine replaced by the GraalVM's one.
The GraalVM team is experimenting with possible ways to save precompiled parts of the JavaScript programs, maybe a standard library, or parts of your application, but when this can become available and in which form is unclear.
AFAICT there are two sides to JS support in GraalVM: "vanilla" JS support and nodejs support.
Oleg's answer here https://stackoverflow.com/a/53749052/202168 explains clearly why building a native image for a nodejs app is not possible.
However it appears that it is possible to build a native image that runs "vanilla" JS:
https://www.graalvm.org/examples/native-image-examples/#polyglot-capabilities
You can also experiment with a more sophisticated ExtListDir example, which takes advantage of GraalVM’s Java and JavaScript polyglot capabilities.
The code for that example is found at:
https://github.com/graalvm/graalvm-demos/blob/master/native-list-dir/ExtListDir.java
In this case there is a Java 'wrapper' for the JS app. I believe this is a necessary part - in other words the GraalVM does not "compile JavaScript to native" but rather it compiles a Java app which includes a Javascript engine implemented in Java.
Maybe this is what Oleg means by "The GraalVM team is experimenting with possible ways to save precompiled parts of the JavaScript programs" i.e. to have some standard Java boilerplate for running JS apps, so you could directly compile a pure JS app to native image. I guess this would effectively be something like Node or Deno but implemented in Java.
The other side of the problem is if you want to use NodeJS features or include code from npm modules in your JS app.
GraalVM states that the Javascript engine implements ES11, basically all of modern JS:
https://www.graalvm.org/reference-manual/js/JavaScriptCompatibility/
It seems like it might be possible to use JS tooling like http://browserify.org/ to convert nodejs JS code into "vanilla" JS that could be run by Graal's JS engine.
Then with appropriate Java wrapper code you could compile all that into a native image.

What is an isomorphic application?

I have been reading multiple different articles about what Isomorphic application is, why this type of architecture is better and so forth. But I still have a bit of uncertainty as to what is meant by such term.
How would you define what "Isomorphic Application" is, without going too much into details?
They are, more recently, also called universal. I'm not sure about the whole app being called isomorphic/universal, but you can certainly have parts of the code base that is universal.
Isomorphic/universal code is code that runs on both the client (browser) and on the server (NodeJS). Since they are both JavaScript this something that is possible if:
you do not mention window, document or any other browser-only methods
you do not mention server, fs or any or any other node-only methods.
If you do need to do the above within some code that is meant to be universal, you should wrap it in a function that either mocks the required method within the alternate environment or wrap it in conditionals so that the app doesn't crash.
An example is console.log which will work both within NodeJS and any browser, along with most other es6 methods in modern browsers.
I use build tools (like webpack) to then help create / export functions within individual files so that we then have a bundle like client-app.js which is included in the HTML file and is the browser only js. The server then might start using server-app.js which is the server-only bundle. Both bundles can be created using a lot of the same universal source code.

Native Plugin For Windows 8 Using Cordova

I am creating a windows8 plugin in cordova but i want to be integarte native build like .winmd with my plugin so that through Winjs i m calling the functions of that library which is in c#. I had already added the reference and code into plugin folder and as well as in project. But still it throws an error WinRt Error : class is not register/JavaScript Error : class in not defined.
I am creating same plugin in WP but there is issue in windows8 plugin development using cordova winjs with native functionality. please help to resolve this issue, provide some samples , links etc.
Updated:
It seems that to reference a WinRT component from a Cordova project using the current VS Tools for Cordova release, you need to follow the instructions here:
https://msopentech.com/blog/2014/11/24/using-windows-runtime-component-with-cordova-project/
In particular, section 1 describes how to inform the Cordova build tools about your WinRT component(s), so that it can reference it/them in the AppX manifest, and include the component binaries in the output package.
Earlier notes:
Do you need to actually write it as a Cordova plug-in? Windows JS apps support really simply integration of custom WinRT components, which can be written in C++ or C#. You just have to create a new Windows Runtime Component project in your solution, define a public sealed class with the methods/properties you want to expose, and then add a reference from your JS app project to the WinRT component (right-click References, select Add Reference, and choose the component project).
That said, be wary of the performance implications of including a C# component (particularly if you need to load it during startup) and the memory usage implications of loading the CLR and all the .NET dependencies. Also remember that C# is not "native" so it isn't really a "native plugin" if you do that :-)
You also might get slightly better advice if you describe your goal (i.e. you need the native plugin for something where you're actually running against JS performance limitations - which is a fairly exceptional case).

How can I use a dll compiled with .Net Framework 4.0in a Windows Metro App

I am new to Windows 8 app development and professional C# dev. I have run into a problem where a client wants me to interface with a printer (DYMO 450 Turbo) and the SDK only comes with a dll that has been compiled using .net 4.0 . The samples show referencing the dll from the app which I can not do because the app is written in Javascript using WinJS. The metro apps use NETCore where the dll uses NETFramework. My best solution so far is to write the code to interface with the printer as a server in .NET 4.0 that listens for requests sent from Javascript over XMLHttpRequest. My gut tells me there HAS to be a better way, but either Ihavn't figured it out or maybe I just don't know enough to figure it out yet. Any help would be appreciated. Thanks in advance.
You cannot use a standard .NET 4.0 assembly in a Windows Store app. You can only use WinRT components. That said, your existing component may or may not be easy to convert to a WinRT componenet. If you simply include the project for your .NET 4.0 component and then change the Output Type (under the project properties) to Windows Runtime Component, you can have a look at all of the compiler errors that are thrown and get an idea of how big of a chore it's going to be to convert it. This article will let you know what the restrictions are on WinRT components.
Suggest add a project using project template c# - windows runtime component, to build a winrt component in c# that can use your sdk dll. winrt component can expose a wrapped impl. winrt component can be referenced in the winjs project.
I have used this for simple utils functions but not for integrating with third party sdk. suggest you give it a shot.

Javascript for command line utilities

Given a need to write command line utilities to do common tasks like uploading files to a remote FTP site, downloading data from a remote MySQL database etc.
Is it practical to use JavaScript for this sort of thing? I know there are JavaScript interpreters that can be run from the command line, but are there libraries for things like FTP and database access the way there are for e.g. Java? If so, what's the best place to look for them? (Google searches with JavaScript in the keywords always seem to return many pages of browser specific things.)
And is there a way to package a JavaScript program up as a standalone executable on Windows?
Update: I've decided Python is a better tool for this kind of job, but the answers to the original question are still good ones.
Standalone executable?
By the way you ask the question, I'm not sure if you are aware, but the Windows Script Host - included in Windows - allows you to run .js files from the command-line. Your javascript will not be an executable, it will remain a script, a text file. The script runs within cscript.exe, which is provided by WSH. There's no compilation required. Maybe you knew all that.
I use Javascript this way for various utilities on Windows.
I think your instinct is right on the availability of libraries. You are sort of on your own to find all those things. Although, once you find them, it's not hard to package Javascript libraries as COM components and allow re-use from anywhere. See here for an example of packaging the Google Diff/Patch/Match Javascript library in COM.
Addendum: Once a bit of code is available within COM, it can be consumed by any Javascript running on the machine. Some examples of COM objects available to Javascript scripts running in WSH:
MSXML2.XMLHTTP object - used in AJAX, but can be used for any HTTP communication. There also an object for the XSLT engine so you can do transforms from script.
Excel.Application - allows you to open up Excel spreadsheets and automate them from Javascript.
Communicator.UIAutomation - automate MS Communicator (send IM's via script)
COM objects for Google Earth.
SlowAES - an all-Javascript implementation of AES encryption.
You can use Rhino to compile Javascript into Java byte code, and get access to all Java libraries.
Or you could use JScript.net, and get access to the .net libraries. .net includes a jsc.exe that produces exe-files.
Both of these requires the respective framework to be installed to be able to run.
Node.js is by far the best environment for running non-browser JS. I've used Rhino and SpiderMonkey, and there's a pretty huge difference in everything from the basics like how errors are handled to the size of the community using the tool. Node is pitched for "server-side" JS - building server apps in JS. It's great for this. But it works equally well for building command line tools.
The NPM package manager (bundled with Node) provides a nice global directory for finding and installing packages. It works much better than other language equivalents like PECL / Pear / CPAN / etc. Several high quality tools like JSHint, The Jade templating language, and the CoffeeScript compiler are all already available through NPM/Node:
npm install -g jshint, coffee-script, jade
jshint my_code.js
jade < my.jade > my.html
For args parsing, there are packages like commander.js. I currently use a heavily extended version of Commander in my underscore-cli command-line tool.
For messing with JSON or for doing command-line JS work (similar to "perl -pe"), check out underscore-cli - It's a really powerful tool for processing JSON data, processing underscore templates, and running JS expressions from the command-line. I use it for 1001 different things that would otherwise be really annoying to accomplish.
Rhino is bundled with JDK 1.6, jrunscript.exe in the bin directory will allow you to run any Javascript you want. Since it runs under Java you get access to any Java libraries that you may have.
We use it from the command line extensively. It's very good at that.
One way is to write these utilities as AIR applications - They can be written in JavaScript and need not have a UI. They have access to the command line, and there are existing ActionScript 3 libraries that can handle FTP etc. These ActionScript APIs can be called from JS, in AIR applications. AIR applications also have access to a sqlite database.
jslibs is a good standalone JavaScript runtime that support many 3rd party open source libraries like zlib, SQLite, NSPR, libiconv, libTomCrypt, OpenGL, ...

Categories

Resources