Dart main() startup sequence - dart

It seems that Dart applications execute their compiled main() function in a thread (isolate) long after the enclosing HTML page has loaded and other scripts on the page have completed. This makes it impossible to inject functions into the document for use by other web components.
e.g. I have UI components that initialize themselves using jquery setup... I'd like to have these UI components delegate to a controller written in dart and this works just fine as long as I set them up in the right order. I could go so far as to call back from dart into JS and run my UI setup but... this is just a bridge to far... (I've already talked myself down from using web components, etc. and resigned myself to using Dart as one big controller of JS functions for now... but if I have to jump through hoops to get even that to work... sigh.)
1) Is there some way to get dart to initialize itself synchronously or to control the initialization process?
2) Is there at least some logical boundary where dart is initializing itself? Some event that I should be targeting for all mysetup after which I know it has run?
Note: this has nothing to do with where the script tag invoking dart is placed or whether it is set to sync or async... Dart seems to spawn an isolate and invoke main() at a later time intentionally.
UPDATE:
It looks like document.ready does fire after main, although I'm not 100% sure it's consistent.
UPDATE 2:
It appears that the packages/browser/dart.js bootstrap loader is causing the delay. I have found that I don't need this and I can simply import the compiled dart js files directly and they work as expected. e.g. if my app is helloworld.dart I can just load and it behaves as expected. Perhaps this should have been obvious, but I had not seen anyone utilize the standalone compiled dart in this way before.
As a bonus I should note that you do not need pub to build these you can just link packages into the source dir and build your JS with dart2js -o outfile. Again, perhaps obvious but without knowing what else the build or bootstrapper does you may not think to try this.

Related

Does compiled AngularDart pollutes global scope or overrides Standard objects of the browser?

I'm looking for a framework that will allow me to write a SPA and a embeddable library. I would love to have a way to share component between both. So I'm looking for a solution that has relatively small amount of potential conflicts with other frameworks and with AngularDart it self. Including case when library has been included using script tab, yes two versions of AngularDart on the same page. A framework that has less Global Objects, no Standard Object overrides, no Global Event handling and limited polyfill conflicts.
Dart and AngularDart seams what I need, but I also need more details and docs to validate my assumptions. Anything you are able to point out would be very helpful and greatly appreciated (issues, PR, blogs , roadmap, commits, specs, docs)
It's possible to run multiple AngularDart apps on the same page. I've tested AngularDart todo example app embedded in itself. But I need more details on what dart2js is doing and how compiler avoids global scope pollution.
Yes, AngularDart should be well suited for your requirements.
Dart itself shouldn't pollute your scope at all, you can try running dart2js on something trivial (like just print inside main) and verify the code - it creates a closure and executes it, so nothing inside is accessible from outside. There is also no patching of any global JS objects, so you can run it alongside anything without interference. If it's not the case file a bug.
You can run as many AngularDart applications on a single page as you wish. To get them fully isolated you can compile each one separately with dart2js, then they wouldn't be able to access any of each other internals whatsoever.

Unable to drag files after installing communication with python script

I built an electron app that allowed dragging files in, with a jQuery script that just takes some info from that (path) and adds an li to a list. That's it. It worked great.
Then I followed this guide, because the next step is to send that information to a python script that analyzes the files (maybe relevant: when installing zeroRPC I built from sources, didn't rely on the prebuilt fork that's available there).
Now I get this crazy bug where when I drag files into the app my mouse pointer changes to not-allowed and the drop event doesn't fire. It's so weird.
I don't have any code sample to give because I can't really tell which part is wrong. The only changes I've done are the ones in the guide I linked, and they have nothing to do with the front-end. I'm really confused by this. not-allowed? Why?
Well, as suspected, the issue had nothing to do with either the front end or the back end. None of my code, really. It turned out that since I needed to compile some stuff while preparing zeroRPC, I used powershell as an administrator,, and you can't drag files from user-run explorer into an admin-run electron app - which makes sense and is in fact an expected behavior (it just so happened that I encountered this after doing some work, causing me to think the problem was with something I changed in my code).

How can I run dart2js dynamically in a Dart server web app?

I want to compile dart code to JS on-the-fly without invoking dart2js at the command line. Eg., (written in Dart) read in some dart code from a file and transform it to JS (must be in memory, filesystem is not writable).
I thought maybe dart2js would effectively just be a cli over a pub package I can call manually, but I can't find any information on doing this at runtime :(
(note: I know this idea sucks and it'll be very slow; it's just for something I'm prototyping and will ultimately use dart2js normally, I just can't address that yet)
https://try-dart-lang.appspot.com/ does this. The source is available. Its basically dart2js run through dart2js.
Not sure if this is the right repository https://github.com/peter-ahe-google/orphan-try
I guess Peter would be ok with pinging him about more information.
The project was replaced by pub.dartlang.org which uses a service running on the server https://github.com/dart-lang/dart-services where source is posted to for dart2js translation.

Dart: Possible to exchange source code on the fly in a running system?

In this article it says: "The Dart VM reads and executes source code, which means there is no compile step between edit and run.". Does that mean that you can exchange source-code on the fly in a running Dart system like in Erlang? Maybe the compiler is removed from the runtime system and then this is no longer possible. So that's why I'm asking.
Dart is run "natively" only in Dartium, which is a flavour of Chrome with DartVM. When you develop an application you still need to compile it it to JavaScript. This way you get fast development lifecycle and in the end you can compile code to JS. Because it's compiled code there is lots more room for compiler to run optimisations on the code. So from my perspective, the compiler is still there and I don't think you would be able to replace code at runtime.
You can send around source code and run it, but it would need to be in a separate isolate. Isolates do have some relationship to Erlang concepts.
The Dart VM doesn't support hot swapping (Called live edit in V8). However, based on mailing list discussions, it sounds like this is something that the authors do want to support in the future.
However, as the others have mentioned, it is possible to dynamically load code into another isolate.

Loading pre-compiled script in RemObjects Pascal Script (Delphi)

I am trying to load a pre-complied RemObjects Pascal Script in Delphi at run-time.
However when I try to load it Delphi excepts with 'Cannot Import VALUE_TEAMCODE.' Value_TeamCode is a function in my Delphi app that I have already registered with Pascal Script.
Here is what I am doing. Rough pseudo code below - actual code is split over multiple files. Also the SetCompiled call below occurs much later in the app when the script is required to run.
Note regarding code:
FPascalScript is a TPSScriptDebugger
...
//Register custom functions with Pascal Script
FuncsRegister;
//Load script
FPascalScript.Script.AddStrings(AContent);
//Compile script
FPascalScript.Compile;
//Get compiled script
FPascalScript.GetCompiled(sCompiledScript)
//Try and set script back in - ERROR Here 'Cannot Import VALUE_TEAMCODE'
FPascalScript.PascalScript.Debugger.SetCompiled(sCompiledScript);
...
Maybe I am going about this wrong. I am not sure if it is even possible to load a pre-compiled script.
I searched on RemObjects WebSite Wiki but the Pascal Script help is deleted.
I also searched various topics here on StackOverflow but none appear to be related to this issue.
Just another note. I already have scripts compiling and executing at run-time with no issues. I need to pre-compile for performance reasons.
Any help appreciated.
Update:
Current work around is to have one script engine per script in my system. These engines then stay in memory after pre-compilation. This removes the 30ms per script compilation overhead I have otherwise. It also uses bit more memory but not enough to be a concern.
I would still rather use just the one script engine though.(Hence the need to load pre-compiled script)
Thanks to a response over on RemObject Connect beta forum I have a solution.
(For post see http://connect.remobjects.com/discussion/comment/13540#Comment_13540)
Thanks go to poster vovanl.
I had to import my functions via the OnExecImport event as follows:
...
FPascalScript.OnExecImport := OnExecImport;
FPascalScript.SetCompiled(sCompiledScript);
...
TMyClass.OnExecImport(Sender: TObject; se: TPSExec; x: TPSRuntimeClassImporter);
begin
se.RegisterDelphiFunction(#Value_TeamCode, 'Value_TeamCode', cdRegister);
end;
...
It appears SetCompiled clears all existing registrations and so you MUST hook OnExecImport to re-register functions, procedures, methods etc.
Note that it appears loading pre compiled script (ie changing out one script for another) does appear to add some extra time overhead. I have found my initial work around is in fact faster by around 6 times.
Rather than the how of this, I am going to counter with why?
Compiled scripts will likely be version bound, probably even platform/target bound - and they usually compile fast enough that you never notice the time hit. Are you really use the scripts intensely enough that compile time is an issue?
Sometimes the best answer is "do you really need to do this at all?"

Resources