Added JS files.
[nit.git] / contrib / online_ide / www / js / pnacl_js.js
1 // This file is part of pnacl_nit ( http://www.pnacl_nitlanguage.org )
2 //
3 // Copyright 2014 Johan Kayser <kayser.johan@gmail.com>
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16
17 pnacl_nitModule = null; // Global application object.
18 var github_acces_token = '9d9412ce7f8223348287aac676b2835baa8f0cfc'; // Github API token.
19 var lib_files = []; // Stores the Nit lib files in JS.
20 var lib_files_number = 0;
21 var sha_filename_map = {}; // Map that stores files with their 'sha' as key and 'name' as value
22 var lib_files_loaded = false;
23 var terminal = null; //terminal object.
24
25 // Indicate load success.
26 function moduleDidLoad() {
27 pnacl_nitModule = document.getElementById('pnacl_nit');
28 if (pnacl_nitModule != null) {
29 // When the module is loaded, we load the lib and init the terminal.
30 console.log('pnacl_nitModule loaded.');
31 load_nit_lib();
32 init_terminal();
33 }
34 }
35
36 // The 'message' event handler. This handler is fired when the NaCl module
37 // posts a message to the browser by calling PPB_Messaging.PostMessage()
38 // (in C) or pp::Instance.PostMessage() (in C++). This implementation
39 // displays the result in the JS console, puts the result in the '#rez' input and make it visible.
40 function handleMessage(message_event) {
41 if (message_event.data.hasOwnProperty('exit')){
42 // If the code exited, shows the error code and reloads Nit lib after the launch of a new thread.
43 console.log('Nit code exited with value: ' + message_event.data.exit + '.');
44 console.log(message_event.data.exit_thread);
45 load_nit_lib();
46 }
47 else if (!message_event.data.hasOwnProperty('operation'))
48 {
49 if(message_event.data == "\n") {
50 // Do nothing.
51 }
52 else {
53 terminal.echo(message_event.data);
54 }
55 }
56 else if (message_event.data.operation == 'load_response'){
57 // We use this to count how many files were loaded for the lib in PNaCl,
58 // if the number matches the number of files loaded from Github,
59 // we know we have all the files.
60 if (message_event.data.files_number == lib_files_number) {
61 console.log('Nit library loaded (' + message_event.data.files_number + ' files), ready to go.');
62 lib_files_loaded = true;
63 }
64 }
65 }
66
67 // Called if the application calls exit(n), abort(), or crashes.
68 function handleCrash() {
69 // We remove the modules and add a new one.
70 $( "#pnacl_nit" ).remove();
71 $( "#listener" ).append("<embed id='pnacl_nit' width=0 height=0 src='/pnacl/pnacl_nit.nmf' type='application/x-pnacl' />");
72 }