This commit is contained in:
Marco Castelluccio 2014-08-05 14:44:45 -07:00
Родитель ccc54a4d1d
Коммит 8d08f25e05
2 изменённых файлов: 174 добавлений и 15 удалений

Просмотреть файл

@ -1,17 +1,24 @@
'use strict';
function Buffer() {
this.pos = 0;
this.array = new Uint8Array(1024);
}
var fs = (function() {
function cleanup(path) {
if (path.length != 1 && path.lastIndexOf("/") == path.length-1) {
path = path.substring(0, path.length-1);
}
return path;
}
function dirname(path) {
var index = path.lastIndexOf("/");
if (index == -1) {
return ".";
}
if (index == path.length-1) {
path = path.substring(0, index);
index = path.lastIndexOf("/");
}
while (index >= 0 && path[index] == "/") {
--index;
}
@ -24,14 +31,7 @@ var fs = (function() {
}
function basename(path) {
var index = path.lastIndexOf("/");
if (index == path.length-1) {
path = path.substring(0, index);
index = path.lastIndexOf("/");
}
return path.slice(index + 1);
return path.slice(path.lastIndexOf("/") + 1);
}
function init(cb) {
@ -47,6 +47,8 @@ var fs = (function() {
var openedFiles = [];
function open(path, cb) {
path = cleanup(path);
asyncStorage.getItem(path, function(blob) {
if (blob == null || !(blob instanceof Blob)) {
cb(-1);
@ -54,6 +56,7 @@ var fs = (function() {
var fd = openedFiles.push({
path: path,
blob: blob,
buffer: new Buffer(),
}) - 1;
cb(fd);
}
@ -115,6 +118,8 @@ var fs = (function() {
}
function list(path, cb) {
path = cleanup(path);
asyncStorage.getItem(path, function(files) {
if (files == null || files instanceof Blob) {
cb(null);
@ -125,6 +130,8 @@ var fs = (function() {
}
function exists(path, cb) {
path = cleanup(path);
asyncStorage.getItem(path, function(data) {
if (data == null) {
cb(false);
@ -135,6 +142,8 @@ var fs = (function() {
}
function truncate(path, cb) {
path = cleanup(path);
asyncStorage.getItem(path, function(data) {
if (data == null || !(data instanceof Blob)) {
cb(false);
@ -147,6 +156,8 @@ var fs = (function() {
}
function remove(path, cb) {
path = cleanup(path);
list(path, function(files) {
if (files != null && files.length > 0) {
cb(false);
@ -175,6 +186,8 @@ var fs = (function() {
}
function createInternal(path, data, cb) {
path = cleanup(path);
var name = basename(path);
var dir = dirname(path);
@ -202,6 +215,8 @@ var fs = (function() {
}
function size(path, cb) {
path = cleanup(path);
asyncStorage.getItem(path, function(blob) {
if (blob == null || !(blob instanceof Blob)) {
cb(-1);

148
native.js
Просмотреть файл

@ -478,7 +478,11 @@ Native["com/sun/cldc/io/ResourceInputStream.bytesRemain.(Ljava/lang/Object;)I"]
Native["com/sun/cldc/io/ResourceInputStream.readByte.(Ljava/lang/Object;)I"] = function(ctx, stack) {
var handle = stack.pop();
stack.push(handle.data[handle.pos++]);
if (handle.data.length - handle.pos <= 0) {
stack.push(-1);
} else {
stack.push(handle.data[handle.pos++]);
}
}
Native["com/sun/cldc/io/ResourceInputStream.readBytes.(Ljava/lang/Object;[BII)I"] = function(ctx, stack) {
@ -564,6 +568,7 @@ Native["com/ibm/oti/connection/file/Connection.fileSizeImpl.([B)J"] = function(c
var path = "/" + new TextDecoder().decode(byteArray);
fs.size(path, function(size) {
console.log(path + " size " + size);
stack.push2(Long.fromNumber(size));
ctx.resume();
});
@ -600,7 +605,7 @@ Native["com/ibm/oti/connection/file/Connection.listImpl.([B[BZ)[[B"] = function(
var pathsArray = CLASSES.newArray("[B", files.length);
for (var i = 0; i < files.length; i++) {
var curPath = path + files[i];
var bytesCurPath = new TextEncoder.encode(curPath);
var bytesCurPath = new TextEncoder().encode(curPath);
var pathArray = CLASSES.newPrimitiveArray("B", bytesCurPath.byteLength);
for (var j = 0; j < bytesCurPath.byteLength; j++) {
pathArray[j] = bytesCurPath[j];
@ -623,9 +628,148 @@ Native["com/ibm/oti/connection/file/Connection.mkdirImpl.([B)I"] = function(ctx,
// IBM's implementation returns different error numbers, we don't care
fs.mkdir(path, function(created) {
console.log(created + " created " + path);
stack.push(created ? 0 : 42);
ctx.resume();
});
throw VM.Pause;
}
Native["com/ibm/oti/connection/file/Connection.newFileImpl.([B)I"] = function(ctx, stack) {
var byteArray = stack.pop(), _this = stack.pop();
var path = "/" + new TextDecoder().decode(byteArray);
// IBM's implementation returns different error numbers, we don't care
fs.exists(path, function(exists) {
console.log(exists + " exists " + path);
if (exists) {
fs.truncate(path, function(truncated) {
console.log(truncated + " truncated " + path);
stack.push(truncated ? 0 : 42);
ctx.resume();
});
} else {
fs.create(path, new Blob(), function(created) {
console.log(created + " created " + path);
stack.push(created ? 0 : 42);
ctx.resume();
});
}
});
throw VM.Pause;
}
Native["com/ibm/oti/connection/file/FCOutputStream.closeImpl.(I)V"] = function(ctx, stack) {
var fd = stack.pop(), _this = stack.pop();
fs.flush(fd, function() {
fs.close(fd);
ctx.resume();
});
throw VM.Pause;
}
Native["com/ibm/oti/connection/file/FCOutputStream.openImpl.([B)I"] = function(ctx, stack) {
var byteArray = stack.pop(), _this = stack.pop();
var path = "/" + new TextDecoder().decode(byteArray);
function open() {
fs.open(path, function(fd) {
stack.push(fd);
_this.pos = 0;
ctx.resume();
});
}
fs.exists(path, function(exists) {
if (exists) {
fs.truncate(path, function(truncated) {
if (truncated) {
open();
} else {
stack.push(-1);
ctx.resume();
}
});
} else {
fs.create(path, function(created) {
if (created) {
open();
} else {
stack.push(-1);
ctx.resume();
}
});
}
});
throw VM.Pause;
}
Native["com/ibm/oti/connection/file/FCOutputStream.openOffsetImpl.([BJ)I"] = function(ctx, stack) {
var offset = stack.pop2(), byteArray = stack.pop(), _this = stack.pop();
var path = "/" + new TextDecoder().decode(byteArray);
function open() {
fs.open(path, function(fd) {
stack.push(fd);
_this.pos = offset.toNumber();
ctx.resume();
});
}
fs.exists(path, function(exists) {
if (exists) {
open();
} else {
fs.create(path, function(created) {
if (created) {
open();
} else {
stack.push(-1);
ctx.resume();
}
});
}
});
throw VM.Pause;
}
Native["com/ibm/oti/connection/file/FCOutputStream.syncImpl.(I)V"] = function(ctx, stack) {
var fd = stack.pop(), _this = stack.pop();
fs.flush(fd, function() {
ctx.resume();
});
throw VM.Pause;
}
Native["com/ibm/oti/connection/file/FCOutputStream.writeByteImpl.(II)V"] = function(ctx, stack) {
var fd = stack.pop(), val = stack.pop(), _this = stack.pop();
var intBuf = new Uint32Array(1);
intBuf[0] = val;
var buf = new Uint8Array(intBuf.buffer);
fs.write(fd, new Blob([buf]).slice(3, 4), _this.pos);
_this.pos += 1;
}
Native["com/ibm/oti/connection/file/FCOutputStream.writeImpl.([BIII)V"] = function(ctx, stack) {
var fd = stack.pop(), count = stack.pop(), offset = stack.pop(), byteArray = stack.pop(), _this = stack.pop();
fs.write(fd, new Blob([byteArray]).slice(offset, offset+count), _this.pos);
_this.pos += count;
}