-
Notifications
You must be signed in to change notification settings - Fork 77
/
vm.plugins.file.browser.js
368 lines (367 loc) · 15.5 KB
/
vm.plugins.file.browser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
"use strict";
/*
* Copyright (c) 2013-2024 Vanessa Freudenberg
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
Object.extend(Squeak.Primitives.prototype,
'FilePlugin', {
primitiveDirectoryCreate: function(argCount) {
var dirNameObj = this.stackNonInteger(0);
if (!this.success) return false;
var dirName = this.filenameFromSqueak(dirNameObj.bytesAsString());
this.success = Squeak.dirCreate(dirName);
if (!this.success) {
var path = Squeak.splitFilePath(dirName);
if (Squeak.debugFiles) console.warn("Directory not created: " + path.fullname);
}
return this.popNIfOK(argCount);
},
primitiveDirectoryDelete: function(argCount) {
var dirNameObj = this.stackNonInteger(0);
if (!this.success) return false;
var dirName = this.filenameFromSqueak(dirNameObj.bytesAsString());
this.success = Squeak.dirDelete(dirName);
return this.popNIfOK(argCount);
},
primitiveDirectoryDelimitor: function(argCount) {
var delimitor = this.emulateMac ? ':' : '/';
return this.popNandPushIfOK(1, this.charFromInt(delimitor.charCodeAt(0)));
},
primitiveDirectoryEntry: function(argCount) {
var dirNameObj = this.stackNonInteger(1),
fileNameObj = this.stackNonInteger(0);
if (!this.success) return false;
var sqFileName = fileNameObj.bytesAsString();
var fileName = this.filenameFromSqueak(sqFileName);
var sqDirName = dirNameObj.bytesAsString();
var dirName = this.filenameFromSqueak(sqDirName);
var entries = Squeak.dirList(dirName, true);
if (!entries) {
var path = Squeak.splitFilePath(dirName);
if (Squeak.debugFiles) console.log("Directory not found: " + path.fullname);
return false;
}
var entry = fileName === "." ? [".", 0, 0, true, 0] : entries[fileName];
this.popNandPushIfOK(argCount+1, this.makeStObject(entry)); // entry or nil
return true;
},
primitiveDirectoryLookup: function(argCount) {
var index = this.stackInteger(0),
dirNameObj = this.stackNonInteger(1);
if (!this.success) return false;
var sqDirName = dirNameObj.bytesAsString();
var dirName = this.filenameFromSqueak(sqDirName);
var entries = Squeak.dirList(dirName, true);
if (!entries) {
var path = Squeak.splitFilePath(dirName);
if (Squeak.debugFiles) console.log("Directory not found: " + path.fullname);
return false;
}
if (Squeak.debugFiles && index === 1) {
console.log("Reading directory " + dirName + " with " + Object.keys(entries).length + " entries");
}
var keys = Object.keys(entries).sort(),
entry = entries[keys[index - 1]];
if (sqDirName === "/") { // fake top-level dir
if (index === 1) {
if (!entry) entry = [0, 0, 0, 0, 0];
entry[0] = "SqueakJS";
entry[3] = true;
}
else entry = null;
}
this.popNandPushIfOK(argCount+1, this.makeStObject(entry)); // entry or nil
return true;
},
primitiveDirectorySetMacTypeAndCreator: function(argCount) {
return this.popNIfOK(argCount);
},
primitiveFileAtEnd: function(argCount) {
var handle = this.stackNonInteger(0);
if (!this.success || !handle.file) return false;
this.popNandPushIfOK(argCount+1, this.makeStObject(handle.filePos >= handle.file.size));
return true;
},
primitiveFileClose: function(argCount) {
var handle = this.stackNonInteger(0);
if (!this.success || !handle.file) return false;
if (typeof handle.file === "string") {
this.fileConsoleFlush(handle.file);
} else {
this.fileClose(handle.file);
this.vm.breakOut(); // return to JS asap so async file handler can run
handle.file = null;
}
return this.popNIfOK(argCount);
},
primitiveFileDelete: function(argCount) {
var fileNameObj = this.stackNonInteger(0);
if (!this.success) return false;
var fileName = this.filenameFromSqueak(fileNameObj.bytesAsString());
this.success = Squeak.fileDelete(fileName);
return this.popNIfOK(argCount);
},
primitiveFileFlush: function(argCount) {
var handle = this.stackNonInteger(0);
if (!this.success || !handle.file) return false;
if (typeof handle.file === "string") {
this.fileConsoleFlush(handle.file);
} else {
Squeak.flushFile(handle.file);
this.vm.breakOut(); // return to JS asap so async file handler can run
}
return this.popNIfOK(argCount);
},
primitiveFileGetPosition: function(argCount) {
var handle = this.stackNonInteger(0);
if (!this.success || !handle.file) return false;
this.popNandPushIfOK(argCount + 1, this.makeLargeIfNeeded(handle.filePos));
return true;
},
makeFileHandle: function(filename, file, writeFlag) {
var handle = this.makeStString("squeakjs:" + filename);
handle.file = file; // shared between handles
handle.fileWrite = writeFlag; // specific to this handle
handle.filePos = 0; // specific to this handle
return handle;
},
primitiveFileOpen: function(argCount) {
var writeFlag = this.stackBoolean(0),
fileNameObj = this.stackNonInteger(1);
if (!this.success) return false;
var fileName = this.filenameFromSqueak(fileNameObj.bytesAsString()),
file = this.fileOpen(fileName, writeFlag);
if (!file) return false;
var handle = this.makeFileHandle(file.name, file, writeFlag);
this.popNandPushIfOK(argCount+1, handle);
return true;
},
primitiveFileRead: function(argCount) {
var count = this.stackInteger(0),
startIndex = this.stackInteger(1) - 1, // make zero based
arrayObj = this.stackNonInteger(2),
handle = this.stackNonInteger(3);
if (!this.success || !arrayObj.isWordsOrBytes() || !handle.file) return false;
if (!count) return this.popNandPushIfOK(argCount+1, 0);
var array = arrayObj.bytes;
if (!array) {
array = arrayObj.wordsAsUint8Array();
startIndex *= 4;
count *= 4;
}
if (startIndex < 0 || startIndex + count > array.length)
return false;
if (typeof handle.file === "string") {
//this.fileConsoleRead(handle.file, array, startIndex, count);
this.popNandPushIfOK(argCount+1, 0);
return true;
}
return this.fileContentsDo(handle.file, function(file) {
if (!file.contents)
return this.popNandPushIfOK(argCount+1, 0);
var srcArray = file.contents,
dstArray = array;
count = Math.min(count, file.size - handle.filePos);
for (var i = 0; i < count; i++)
dstArray[startIndex + i] = srcArray[handle.filePos++];
if (!arrayObj.bytes) count >>= 2; // words
this.popNandPushIfOK(argCount+1, Math.max(0, count));
}.bind(this));
},
primitiveFileRename: function(argCount) {
var oldNameObj = this.stackNonInteger(1),
newNameObj = this.stackNonInteger(0);
if (!this.success) return false;
var oldName = this.filenameFromSqueak(oldNameObj.bytesAsString()),
newName = this.filenameFromSqueak(newNameObj.bytesAsString());
this.success = Squeak.fileRename(oldName, newName);
this.vm.breakOut(); // return to JS asap so async file handler can run
return this.popNIfOK(argCount);
},
primitiveFileSetPosition: function(argCount) {
var pos = this.stackPos32BitInt(0),
handle = this.stackNonInteger(1);
if (!this.success || !handle.file) return false;
handle.filePos = pos;
return this.popNIfOK(argCount);
},
primitiveFileSize: function(argCount) {
var handle = this.stackNonInteger(0);
if (!this.success || !handle.file) return false;
this.popNandPushIfOK(argCount+1, this.makeLargeIfNeeded(handle.file.size));
return true;
},
primitiveFileStdioHandles: function(argCount) {
var handles = [
null, // stdin
this.makeFileHandle('console.log', 'log', true),
this.makeFileHandle('console.error', 'error', true),
];
this.popNandPushIfOK(argCount + 1, this.makeStArray(handles));
return true;
},
primitiveFileTruncate: function(argCount) {
var pos = this.stackPos32BitInt(0),
handle = this.stackNonInteger(1);
if (!this.success || !handle.file || !handle.fileWrite) return false;
if (handle.file.size > pos) {
handle.file.size = pos;
handle.file.modified = true;
if (handle.filePos > handle.file.size) handle.filePos = handle.file.size;
}
return this.popNIfOK(argCount);
},
primitiveDisableFileAccess: function(argCount) {
return this.fakePrimitive("FilePlugin.primitiveDisableFileAccess", 0, argCount);
},
primitiveFileWrite: function(argCount) {
var count = this.stackInteger(0),
startIndex = this.stackInteger(1) - 1, // make zero based
arrayObj = this.stackNonInteger(2),
handle = this.stackNonInteger(3);
if (!this.success || !handle.file || !handle.fileWrite) return false;
if (!count) return this.popNandPushIfOK(argCount+1, 0);
var array = arrayObj.bytes;
if (!array) {
array = arrayObj.wordsAsUint8Array();
startIndex *= 4;
count *= 4;
}
if (!array) return false;
if (startIndex < 0 || startIndex + count > array.length)
return false;
if (typeof handle.file === "string") {
this.fileConsoleWrite(handle.file, array, startIndex, count);
this.popNandPushIfOK(argCount+1, count);
return true;
}
return this.fileContentsDo(handle.file, function(file) {
var srcArray = array,
dstArray = file.contents || [];
if (handle.filePos + count > dstArray.length) {
var newSize = dstArray.length === 0 ? handle.filePos + count :
Math.max(handle.filePos + count, dstArray.length + 10000);
file.contents = new Uint8Array(newSize);
file.contents.set(dstArray);
dstArray = file.contents;
}
for (var i = 0; i < count; i++)
dstArray[handle.filePos++] = srcArray[startIndex + i];
if (handle.filePos > file.size) file.size = handle.filePos;
file.modified = true;
if (!arrayObj.bytes) count >>= 2; // words
this.popNandPushIfOK(argCount+1, count);
}.bind(this));
},
fileOpen: function(filename, writeFlag) {
// if a file is opened for read and write at the same time,
// they must share the contents. That's why all open files
// are held in the ref-counted global SqueakFiles
if (typeof SqueakFiles == 'undefined')
window.SqueakFiles = {};
var path = Squeak.splitFilePath(filename);
if (!path.basename) return null; // malformed filename
// fetch or create directory entry
var directory = Squeak.dirList(path.dirname, true);
if (!directory) return null;
var entry = directory[path.basename],
contents = null;
if (entry) {
// if it is open already, return it
var file = SqueakFiles[path.fullname];
if (file) {
++file.refCount;
return file;
}
} else {
if (!writeFlag) {
if (Squeak.debugFiles) console.log("File not found: " + path.fullname);
return null;
}
contents = new Uint8Array();
entry = Squeak.filePut(path.fullname, contents.buffer);
if (!entry) {
if (Squeak.debugFiles) console.log("Cannot create file: " + path.fullname);
return null;
}
}
// make the file object
var file = {
name: path.fullname,
size: entry[4], // actual file size, may differ from contents.length
contents: contents, // possibly null, fetched when needed
modified: false,
refCount: 1
};
SqueakFiles[file.name] = file;
return file;
},
fileClose: function(file) {
Squeak.flushFile(file);
if (--file.refCount == 0)
delete SqueakFiles[file.name];
},
fileContentsDo: function(file, func) {
if (file.contents) {
func(file);
} else {
if (file.contents === false) // failed to get contents before
return false;
this.vm.freeze(function(unfreeze) {
var error = (function(msg) {
console.warn("File get failed: " + msg);
file.contents = false;
unfreeze();
func(file);
}).bind(this),
success = (function(contents) {
if (contents == null) return error(file.name);
file.contents = this.asUint8Array(contents);
unfreeze();
func(file);
}).bind(this);
Squeak.fileGet(file.name, success, error);
}.bind(this));
}
return true;
},
fileConsoleBuffer: {
log: '',
error: ''
},
fileConsoleWrite: function(logOrError, array, startIndex, count) {
// buffer until there is a newline
var bytes = array.subarray(startIndex, startIndex + count),
buffer = this.fileConsoleBuffer[logOrError] + Squeak.bytesAsString(bytes),
lines = buffer.match('([^]*)\n(.*)');
if (lines) {
console[logOrError](lines[1]); // up to last newline
buffer = lines[2]; // after last newline
}
this.fileConsoleBuffer[logOrError] = buffer;
},
fileConsoleFlush: function(logOrError) {
var buffer = this.fileConsoleBuffer[logOrError];
if (buffer) {
console[logOrError](buffer);
this.fileConsoleBuffer[logOrError] = '';
}
},
});