1 // This file is part of NIT ( http://www.nitlanguage.org )
3 // Copyright 2014 Johan Kayser <kayser.johan@gmail.com>
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 // Initializes the terminal, set it to send messages to PNaCl.
18 function init_terminal() {
19 terminal
= $
('#terminal').terminal(function(command
, term
) {
20 pnacl_nitModule
.postMessage(command
+ '\n');
21 }, { greetings
: false, name
: 'terminal' });
24 // Edit current file name.
25 $
('#filename').bind('click', function() {
26 $
(this).attr('contentEditable', true);
29 $
(this).attr('contentEditable', false);
32 // To launch the interpret process.
33 function interpret() {
35 operation
: 'interpret',
36 args
: /*"-v -v " + */document
.getElementById('filename').innerText
+ ".nit",
37 content
: editor
.getValue()
39 pnacl_nitModule
.postMessage(dictionary
);
42 // To save the current editor lines in a file.
44 var blob
= new Blob([editor
.getValue()], {type
: "text/plain;charset=utf-8"});
45 saveAs(blob
, document
.getElementById('filename').innerText
+ ".nit");
49 function load_nit_lib() {
50 if (!lib_files_loaded
)
52 // We get the 'nit' folder content on github.
53 $
.get("https://api.github.com/repos/privat/nit/contents/?access_token=" + github_acces_token
, function(data
) {
54 for (var i
= 0; i
< data
.length
; i
++) {
55 if (data
[i].name
== "lib") {
56 // We get the list of all files in the 'lib' folder.
57 $
.get("https://api.github.com/repos/privat/nit/git/trees/" + data
[i].sha
+ "?recursive=1&access_token=" + github_acces_token
, function(data
) {
58 for (var i
= 0; i
< data
.tree
.length
; i
++) {
59 if (data
.tree
[i].type
== "blob") {
61 sha_filename_map
[data
.tree
[i].sha
] = data
.tree
[i].path
.split("/").pop();
63 // We get the content of each file and send it to PNaCl.
64 $
.get(data
.tree
[i].url
+ "?access_token=" + github_acces_token
, function(data
) {
67 filename
: sha_filename_map
[data
.sha
],
68 content
: Base64
.decode(data
.content
),
70 pnacl_nitModule
.postMessage(dictionary
);
71 lib_files
.push(dictionary
);
81 // If we already have the files stored in JS, we just send them.
82 for (var i
= 0; i
< lib_files
.length
; i
++) {
83 pnacl_nitModule
.postMessage(lib_files
[i]);
88 // To code/decode base64
90 _keyStr
: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
92 encode
: function(input
) {
94 var chr1
, chr2
, chr3
, enc1
, enc2
, enc3
, enc4
;
97 input
= Base64
._utf8_encode(input
);
98 while (i
< input
.length
) {
99 chr1
= input
.charCodeAt(i
++);
100 chr2
= input
.charCodeAt(i
++);
101 chr3
= input
.charCodeAt(i
++);
103 enc2
= ((chr1
& 3) << 4) |
(chr2
>> 4);
104 enc3
= ((chr2
& 15) << 2) |
(chr3
>> 6);
108 } else if (isNaN(chr3
)) {
111 output
= output
+ this._keyStr
.charAt(enc1
) + this._keyStr
.charAt(enc2
) + this._keyStr
.charAt(enc3
) + this._keyStr
.charAt(enc4
);
117 decode
: function(input
) {
119 var chr1
, chr2
, chr3
;
120 var enc1
, enc2
, enc3
, enc4
;
123 input
= input
.replace(/[^A
-Za
-z0
-9\
+\
/\
=]/g
, "");
124 while (i
< input
.length
) {
125 enc1
= this._keyStr
.indexOf(input
.charAt(i
++));
126 enc2
= this._keyStr
.indexOf(input
.charAt(i
++));
127 enc3
= this._keyStr
.indexOf(input
.charAt(i
++));
128 enc4
= this._keyStr
.indexOf(input
.charAt(i
++));
129 chr1
= (enc1
<< 2) |
(enc2
>> 4);
130 chr2
= ((enc2
& 15) << 4) |
(enc3
>> 2);
131 chr3
= ((enc3
& 3) << 6) | enc4
;
132 output
= output
+ String
.fromCharCode(chr1
);
134 output
= output
+ String
.fromCharCode(chr2
);
137 output
= output
+ String
.fromCharCode(chr3
);
140 output
= Base64
._utf8_decode(output
);
144 _utf8_encode
: function(string
) {
145 string
= string
.replace(/\r\n/g
, "\n");
148 for (var n
= 0; n
< string
.length
; n
++) {
149 var c
= string
.charCodeAt(n
);
152 utftext
+= String
.fromCharCode(c
);
154 else if ((c
> 127) && (c
< 2048)) {
155 utftext
+= String
.fromCharCode((c
>> 6) |
192);
156 utftext
+= String
.fromCharCode((c
& 63) |
128);
159 utftext
+= String
.fromCharCode((c
>> 12) |
224);
160 utftext
+= String
.fromCharCode(((c
>> 6) & 63) |
128);
161 utftext
+= String
.fromCharCode((c
& 63) |
128);
167 _utf8_decode
: function(utftext
) {
172 while (i
< utftext
.length
) {
173 c
= utftext
.charCodeAt(i
);
175 string
+= String
.fromCharCode(c
);
178 else if ((c
> 191) && (c
< 224)) {
179 c2
= utftext
.charCodeAt(i
+ 1);
180 string
+= String
.fromCharCode(((c
& 31) << 6) |
(c2
& 63));
184 c2
= utftext
.charCodeAt(i
+ 1);
185 c3
= utftext
.charCodeAt(i
+ 2);
186 string
+= String
.fromCharCode(((c
& 15) << 12) |
((c2
& 63) << 6) |
(c3
& 63));