Can I use a Dart interface from manually written JavaScript? - javascript

I consider using Dart for my next web project and it has made a very neat impression on me so far. Obviously, Dart code can be converted into JavaScript. However, I wonder if this results in code where functions can be used from other JavaScript files.

It is supported to call Dart functions from JavaScript but only functions that are made available to JavaScript explicitely.
See
Calling Dart code from javascript
Expose Dart functions to javascript
When you build Dart to JavaScript minification and tree-shaking make it hard or mostly impossible to call methods because functions might not even be included in the output if the analysis recognized that the function isn't called anywhere and if it actually is included it will have a shortened and cryptic name.
I have seen it mentioned that here are attempts for better support of this use case but there isn't anything available yet.

If your generated JavaScript is accessible then for sure it will regardless of it is generated using dart or anything else

Related

Is there any way to delete useless functions in JS libraries?

I'm building a project in javascript, using paper.js in some features.
Most of my project is built in vanilla Javascript, just some minimum but important features are built with paper.js
The problem is that the library (.min library) is 200kb.
The normal library is 300kb, I was wondering if there is an automatic way to see which functions are being used in the main paper.js library, in order to delete the useless functions.
If there is no program or automatic way to do this, maybe some advice of how to do it manually, or which tools you recommend for me and my team in order to delete useless functions, then minify the file and run it smaller.
Thank you all guys, I did not added any specific code because I want this anwser to be global.
Greetings
You have to do it manually but it's not an easy (and certainly not quick) process. You'll have to find which functions you're using and then find whatever classes or functions those functions reference. You would probably have an easier time creating a new script then copy/pasting what you're using (and any referenced content) then running it with your script, log errors, and repeat.
When you're done there's many minify libraries and services online you can use to minify the new script.
I used the paper-core version and then minified.
I saved 140kb by doing this.
There's still no way to see useless functions in this library

Compilers vs Interpreters in terms of code analysis

I started being a developer using Java and got used to powerful IDEs which support me when writing my source code. Being more precise, e.g. when trying to use a function that does not exist in Eclipse, I immediately get an error when typing this, without running my application. How does Eclipse does this? By pre-compiling my code?
Nowadays developing JavaScript or Node.js, I still don't understand why such features are not existing in an IDE using an interpreted programming language. I understand that an interpreter analyzes the code during execution. But why is it not possible e.g. to build a plugin for an IDE as Visual Studio Code which pre-analyses my code while writing it? I would really appreciate getting an error when trying to call a function in JS that does not exist, for example. Also a linter does not help in such cases.
Anyone can support my understanding with this? Thank you a lot :)
An IDE can analyse your code, and it could warn you that you have appear not to have defined a function which you appear to be calling. But in many cases, you might want to tell the IDE to take a hike and stop bugging you, because the function definitely exists. It's just not visible to the IDE.
C++ and Java don't permit that case. If you call a function by name, the compiler must be able to see that function. (In C++, the compiler doesn't really know whether or not that function will eventually be included in your project; it may be that all it knows is whether you have included a header which declares a prototype for the function. But you must have done at least that, and so the compiler -- and the IDE -- have good reason to assume that you intend that function to be part of the executable.
Java is even stricter. If the compiler can't see the other class modules you're using, it won't compile your function. (With C++ you could compile your function (as long as you have the header with the functions it is calling) before you even write the functions being used, although you cannot create the executable until you have all the necessary pieces available.)
But Javascript doesn't require declarations. Javascript doesn't need to know what the argument or result types of a function you're calling are. So it doesn't need to force you to provide that information, ever. When you eventually get around to running the program, the JS interpreter will have to find the functions you are calling, but they don't have to be in the text of the program you wrote. They could be in a different file which you dynamically added to the execution environment, or they could be in a web page into which your Javascript program has been loaded. Or they could even be created on the fly and added to the execution environment with eval. (Or in other ways.)

Are node.js and V8 builtins written in JS or compiled and run as native code?

In node.js and I assume by extension V8, are built-ins (such as JSON, Math, etc) native code residing in the interpreter or are they javascript code that gets interpreted the same way as the rest of user-written code?
So for example, does a call to JSON.stringify(my_data) jump to compiled machine code or does it just run more JS code?
This appears to be responsible for JSON stringification:
https://github.com/joyent/node/blob/master/deps/v8/src/json-stringifier.h
Quickly browsing the repo will give you an idea of whats written in native code (and what's not):
https://github.com/joyent/node/tree/master/deps/v8/src
Glancing at the V8 source code, I think it's a mix of both. JSON is backed by native code in json-parser.h; (at least some of) math is implemented as JavaScript in math.js.

ms:script tags in XSLT

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

JavaScript bytecode compiler?

For a project I'm tangentially working on, I need a way to compile JavaScript into some intermediate language or bytecode so that I can single-step through it. I know that many of the JavaScript engines in modern browsers do something like this, but they don't make the resulting bytecode accessible. Is there a good off-the-shelf tool for this sort of JavaScript compilation?
Not exactly sure of your needs, however maybe Rhino could work for you.
The JavaScript compiler translates
JavaScript source into Java class
files. The resulting Java class files
can then be loaded and executed at
another time, providing a convenient
method for transfering JavaScript, and
for avoiding translation cost.
More about the compile function is located here.

Categories

Resources