Skip to content

Commit

Permalink
jsc/common/thinthin.jsc: Perl changes
Browse files Browse the repository at this point in the history
  • Loading branch information
pipcet committed Jan 31, 2021
1 parent eb86ac2 commit 57687ca
Showing 1 changed file with 54 additions and 26 deletions.
80 changes: 54 additions & 26 deletions jsc/common/thinthin.jsc
Original file line number Diff line number Diff line change
Expand Up @@ -1443,7 +1443,36 @@ ThinThinHalfFD.prototype.readdir = function ()
this.half[1].metareader(new ThinThinMetaReader(["readdir"], resolve, reject));
});
};
//** SparseMetaWriter

ThinThinHalfFD.prototype.write = function (heap, ptr, len)
{
let ret = ThinThinFD.prototype.write.call(this, heap, ptr, len);
if (!this.other.reader_promise) {
return new Promise(r => this.other.reader_promise_promise = r).then(() => {
this.other.reader_promise(this.readData);
return ret;
})
}
this.other.reader_promise(CStringAt(heap, ptr));
return ret;
};

ThinThinHalfFD.prototype.read = function (heap, ptr, len)
{
if (!this.reader) {
this.reader = new Promise(r => {
this.reader_promise = r;
if (this.reader_promise_promise) {
this.reader_promise_promise();
}
});
}
return this.reader.then(data => {
this.readData = data;
return ThinThinFD.prototype.read.call(this, heap, ptr, len);
});
};

function SparseMetaWriter(half)
{
this.half = half;
Expand Down Expand Up @@ -2291,7 +2320,7 @@ WebSocketWriter.prototype.provide = function (n)
return new Promise((resolve, reject) => {
this.resolve = resolve;
this.reject = reject;
reader.readAsArrayBuffer(this.webSocket);
return reader.readAsArrayBuffer(this.webSocket);
}).then(abuf => {
var view = new Uint8Array(abuf);
var s = "";
Expand Down Expand Up @@ -2369,7 +2398,7 @@ FileWriter.prototype.provide = function (n)
reader.onload = () => {
resolve(reader.result);
};
reader.readAsArrayBuffer(this.file);
return reader.readAsArrayBuffer(this.file);
}).then(abuf => {
var view = new Uint8Array(abuf);
var s = "";
Expand Down Expand Up @@ -2880,7 +2909,7 @@ if (typeof(os) !== "undefined" &&
ThinThin.newfstatat = function (fdno, pathstr, statbufptr, flags) {
var dd = this.fds[fdno] || this.process.ddroot;
var path = CStringAt(this.HEAPU8, pathstr);
if (this.pwd)
if (this.pwd && path[0] !== "/")
path = this.pwd + "/" + path;

if (path === "/home/pip/g/wasm/wasm32-unknown-none/wasm32-unknown-none/share/terminfo") {
Expand Down Expand Up @@ -2915,7 +2944,7 @@ if (typeof(os) !== "undefined" &&
});

if ("openat" in dd) {
return dd.openat(path).then(fd => {
return dd.openat(path, flags).then(fd => {
if (typeof fd === "number")
return fd;
var off;
Expand Down Expand Up @@ -3200,11 +3229,18 @@ if (typeof(os) !== "undefined" &&
let args = CStringsAt(this.heap, argvptr);
let command = args.join(" ");
if (this.pwd !== undefined)
command = "cd " + this.pwd + "; " + command;
command = "(cd " + this.pwd + "; PWD=. " + command + ")>tmp.out";
try {
console.log("command",command);
let pid = os.spawn(command);
let o = os.waitpid(pid);
return ThinThin.exit.call(this, o.exitStatus);
let output = os.file.readFile("tmp.out", "binary");
for (let i = 0; i < output.length; i++)
if (output[i] === 32) output[i] = 10;
console.log("output", output);
//this.fds[2].write(output, 0, 0);
let ret = this.fds[1].write(output, 0, output.length);
return new Promise(() => {});
} catch (e) {
console.log("OH NO!");
console.log(e);
Expand All @@ -3213,6 +3249,7 @@ if (typeof(os) !== "undefined" &&
};
ThinThin.wait4 = function(pid, wstatusp, options, rusage)
{
return pid; // XXX, in the fork-exec-wait4 case this works okay.
var ret = this.kport.req("wait4");

if (wstatusp)
Expand Down Expand Up @@ -3446,24 +3483,13 @@ ThinThin.chdir = function (ptr)
ThinThin.pipe2 = function (ptr, flags)
{
var pipe = new ThinThinHalf();
this.HEAP32[ptr >> 2] = (new ThinThinHalfFD(this.process, pipe)).fdno;
this.HEAP32[ptr + 4 >> 2] = (new ThinThinHalfFD(this.process, pipe)).fdno;
return Promise.resolve(0);
return new Promise((resolve, reject) => {
this.half[1].metareader(new ThinThinMetaReader(["openat", path, flags], resolve, reject));
}).then(response => {
var obj = response[0][0];

if ((obj instanceof Array) && obj.length && (obj[0] instanceof File)) {
var pipe = new ThinThinHalf();
new FileListHalf(pipe, obj);
return new ThinThinHalfFD(this.process, pipe);
} else if (obj instanceof File) {
var pipe = new ThinThinHalf();
new FileHalf(pipe, obj);
return new ThinThinHalfFD(this.process, pipe);
}
});
let fds = [new ThinThinHalfFD(this.process, pipe),
new ThinThinHalfFD(this.process, pipe)];
fds[0].other = fds[1];
fds[1].other = fds[0];
this.HEAP32[ptr >> 2] = fds[0].fdno;
this.HEAP32[ptr + 4 >> 2] = fds[1].fdno;
return 0;
};

ThinThin.renameat2 = function (oldfdno, oldpathptr, newfdno, newpathptr, flags)
Expand Down Expand Up @@ -3512,9 +3538,11 @@ ThinThin.openat = function (fdno, ptr, flags, mode) {
}
}

if (this.pwd && path[0] !== "/")
path = this.pwd + "/" + path;
var dd = this.fds[fdno];
if (!dd)
dd = this.process.ddroot;
dd = this.process.ddcwd;

if (path.length === 0 || path === ".")
return Promise.resolve(dd).then(fd => (typeof fd === "object") ? fd.fdno : fd);
Expand Down

0 comments on commit 57687ca

Please sign in to comment.