Compare commits
No commits in common. "f3d419085c999375fdb1f3bb7affe1be0f5e5902" and "f16d08864647ee469afed7749cdc218f518c5b1b" have entirely different histories.
f3d419085c
...
f16d088646
@ -3,5 +3,5 @@ repositories {
|
||||
}
|
||||
|
||||
plugins {
|
||||
`kotlin-dsl`
|
||||
`kotlin-dsl`
|
||||
}
|
||||
|
@ -53,8 +53,8 @@ public abstract class FunctionNode extends Node {
|
||||
target.add(scope.define(param.name).index().toSet(false)).setLocation(param.loc());
|
||||
}
|
||||
if (hasSelf) {
|
||||
target.add(Instruction.loadCalled());
|
||||
target.add(scope.define(selfName).index().toSet(false));
|
||||
target.add(Instruction.loadCalled());
|
||||
target.add(scope.define(selfName).index().toSet(false));
|
||||
}
|
||||
|
||||
body.compile(target, lastReturn, BreakpointType.NONE);
|
||||
|
@ -286,40 +286,40 @@ public final class JavaScript {
|
||||
}
|
||||
|
||||
public static ParseRes<List<VariableNode>> parseParameters(Source src, int i) {
|
||||
var n = Parsing.skipEmpty(src, i);
|
||||
var n = Parsing.skipEmpty(src, i);
|
||||
|
||||
var openParen = Parsing.parseOperator(src, i + n, "(");
|
||||
if (!openParen.isSuccess()) return openParen.chainError(src.loc(i + n), "Expected a parameter list");
|
||||
n += openParen.n;
|
||||
var openParen = Parsing.parseOperator(src, i + n, "(");
|
||||
if (!openParen.isSuccess()) return openParen.chainError(src.loc(i + n), "Expected a parameter list");
|
||||
n += openParen.n;
|
||||
|
||||
var params = new ArrayList<VariableNode>();
|
||||
var params = new ArrayList<VariableNode>();
|
||||
|
||||
var closeParen = Parsing.parseOperator(src, i + n, ")");
|
||||
n += closeParen.n;
|
||||
var closeParen = Parsing.parseOperator(src, i + n, ")");
|
||||
n += closeParen.n;
|
||||
|
||||
if (!closeParen.isSuccess()) {
|
||||
while (true) {
|
||||
n += Parsing.skipEmpty(src, i + n);
|
||||
if (!closeParen.isSuccess()) {
|
||||
while (true) {
|
||||
n += Parsing.skipEmpty(src, i + n);
|
||||
|
||||
var param = VariableNode.parse(src, i + n);
|
||||
if (!param.isSuccess()) return ParseRes.error(src.loc(i + n), "Expected a parameter or a closing brace");
|
||||
n += param.n;
|
||||
n += Parsing.skipEmpty(src, i + n);
|
||||
var param = VariableNode.parse(src, i + n);
|
||||
if (!param.isSuccess()) return ParseRes.error(src.loc(i + n), "Expected a parameter or a closing brace");
|
||||
n += param.n;
|
||||
n += Parsing.skipEmpty(src, i + n);
|
||||
|
||||
params.add(param.result);
|
||||
params.add(param.result);
|
||||
|
||||
if (src.is(i + n, ",")) {
|
||||
n++;
|
||||
n += Parsing.skipEmpty(src, i + n);
|
||||
}
|
||||
if (src.is(i + n, ",")) {
|
||||
n++;
|
||||
n += Parsing.skipEmpty(src, i + n);
|
||||
}
|
||||
|
||||
if (src.is(i + n, ")")) {
|
||||
n++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (src.is(i + n, ")")) {
|
||||
n++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ParseRes.res(params, n);
|
||||
return ParseRes.res(params, n);
|
||||
}
|
||||
}
|
||||
|
@ -17,7 +17,6 @@
|
||||
"typescript": "^5.7.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/plugin-proposal-class-properties": "^7.18.6",
|
||||
"@babel/plugin-transform-class-properties": "^7.25.9",
|
||||
"@babel/plugin-transform-runtime": "^7.25.9",
|
||||
"@babel/plugin-transform-typescript": "^7.25.9",
|
||||
|
@ -34,6 +34,7 @@ const construct = (input, output) => defineConfig({
|
||||
optimizeConstEnums: true,
|
||||
allowDeclareFields: true,
|
||||
}],
|
||||
["@babel/plugin-transform-class-properties"],
|
||||
["@babel/plugin-transform-runtime", {
|
||||
moduleName: shouldPolyfill() ? "!polyfills:" : undefined,
|
||||
version: "^7.24.0",
|
||||
@ -47,7 +48,6 @@ const construct = (input, output) => defineConfig({
|
||||
assumptions: {
|
||||
ignoreToPrimitiveHint: true,
|
||||
noClassCalls: true,
|
||||
privateFieldsAsProperties: true,
|
||||
},
|
||||
|
||||
env: {
|
||||
@ -77,8 +77,7 @@ const construct = (input, output) => defineConfig({
|
||||
"@babel/plugin-transform-optional-chaining",
|
||||
"@babel/plugin-transform-logical-assignment-operators",
|
||||
"@babel/plugin-transform-numeric-separator",
|
||||
"@babel/plugin-transform-private-methods",
|
||||
"@babel/plugin-proposal-class-properties",
|
||||
"@babel/plugin-transform-class-properties",
|
||||
"@babel/plugin-transform-class-static-block",
|
||||
"@babel/plugin-transform-regenerator",
|
||||
|
||||
|
13
lib/src/lib/async.d.ts
vendored
13
lib/src/lib/async.d.ts
vendored
@ -22,16 +22,3 @@ declare interface PromiseConstructor {
|
||||
resolve<T>(val: T): Promise<Awaited<T>>;
|
||||
reject<T>(err: unknown): Promise<T>;
|
||||
}
|
||||
|
||||
declare interface AsyncIterator<T, Return = unknown, Next = unknown> {
|
||||
next(): Promise<IterationData<T, Return>>;
|
||||
next(val: Next): Promise<IterationData<T, Return>>;
|
||||
error?(err: unknown): Promise<IterationData<T, Return>>;
|
||||
return?(val: Return): Promise<IterationData<T, Return>>;
|
||||
}
|
||||
declare interface AsyncIterableIterator<T, Return = unknown, Next = unknown> extends AsyncIterator<T, Return, Next> {
|
||||
[Symbol.iterator](): this;
|
||||
}
|
||||
declare interface AsyncIterable<T> {
|
||||
[Symbol.iterator](): AsyncIterator<T>;
|
||||
}
|
||||
|
6
lib/src/lib/iterator.d.ts
vendored
6
lib/src/lib/iterator.d.ts
vendored
@ -1,12 +1,12 @@
|
||||
declare interface NormalIterationData<T> {
|
||||
value: T;
|
||||
done: false;
|
||||
done: true;
|
||||
}
|
||||
declare interface DoneIterationData<T> {
|
||||
value: T;
|
||||
done?: true;
|
||||
done?: false;
|
||||
}
|
||||
declare type IterationData<T, Return = void> = NormalIterationData<T> | DoneIterationData<Return>;
|
||||
declare type IterationData<T, Return> = NormalIterationData<T> | DoneIterationData<Return>;
|
||||
|
||||
declare interface Iterator<T, Return = unknown, Next = unknown> {
|
||||
next(): IterationData<T, Return>;
|
||||
|
@ -1,37 +0,0 @@
|
||||
package me.topchetoeu.j2s.lib.debug;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import me.topchetoeu.j2s.runtime.debug.DebugHandler;
|
||||
|
||||
public interface Debugger extends DebugHandler {
|
||||
void close();
|
||||
|
||||
void enable(V8Message msg) throws IOException;
|
||||
void disable(V8Message msg) throws IOException;
|
||||
|
||||
void setBreakpointByUrl(V8Message msg) throws IOException;
|
||||
void removeBreakpoint(V8Message msg) throws IOException;
|
||||
void continueToLocation(V8Message msg) throws IOException;
|
||||
|
||||
void getScriptSource(V8Message msg) throws IOException;
|
||||
void getPossibleBreakpoints(V8Message msg) throws IOException;
|
||||
|
||||
void resume(V8Message msg) throws IOException;
|
||||
void pause(V8Message msg) throws IOException;
|
||||
|
||||
void stepInto(V8Message msg) throws IOException;
|
||||
void stepOut(V8Message msg) throws IOException;
|
||||
void stepOver(V8Message msg) throws IOException;
|
||||
|
||||
void setPauseOnExceptions(V8Message msg) throws IOException;
|
||||
|
||||
void evaluateOnCallFrame(V8Message msg) throws IOException;
|
||||
|
||||
void getProperties(V8Message msg) throws IOException;
|
||||
void releaseObjectGroup(V8Message msg) throws IOException;
|
||||
void releaseObject(V8Message msg) throws IOException;
|
||||
void callFunctionOn(V8Message msg) throws IOException;
|
||||
|
||||
void runtimeEnable(V8Message msg) throws IOException;
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
package me.topchetoeu.j2s.lib.debug;
|
||||
|
||||
public interface DebuggerProvider {
|
||||
Debugger getDebugger(WebSocket socket, HttpRequest req);
|
||||
}
|
@ -1,100 +0,0 @@
|
||||
package me.topchetoeu.j2s.lib.debug;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.net.Socket;
|
||||
import java.util.HashMap;
|
||||
import java.util.IllegalFormatException;
|
||||
import java.util.Map;
|
||||
|
||||
import me.topchetoeu.j2s.common.Reading;
|
||||
|
||||
public class HttpRequest {
|
||||
public final String method;
|
||||
public final String path;
|
||||
public final Map<String, String> headers;
|
||||
public final OutputStream out;
|
||||
|
||||
public void writeCode(int code, String name) {
|
||||
try { out.write(("HTTP/1.1 " + code + " " + name + "\r\n").getBytes()); }
|
||||
catch (IOException e) { }
|
||||
}
|
||||
public void writeHeader(String name, String value) {
|
||||
try { out.write((name + ": " + value + "\r\n").getBytes()); }
|
||||
catch (IOException e) { }
|
||||
}
|
||||
public void writeLastHeader(String name, String value) {
|
||||
try { out.write((name + ": " + value + "\r\n\r\n").getBytes()); }
|
||||
catch (IOException e) { }
|
||||
}
|
||||
public void writeHeadersEnd() {
|
||||
try { out.write("\n".getBytes()); }
|
||||
catch (IOException e) { }
|
||||
}
|
||||
|
||||
public void writeResponse(int code, String name, String type, byte[] data) {
|
||||
writeCode(code, name);
|
||||
writeHeader("Content-Type", type);
|
||||
writeLastHeader("Content-Length", data.length + "");
|
||||
try {
|
||||
out.write(data);
|
||||
out.close();
|
||||
}
|
||||
catch (IOException e) { }
|
||||
}
|
||||
public void writeResponse(int code, String name, String type, InputStream data) {
|
||||
writeResponse(code, name, type, Reading.streamToBytes(data));
|
||||
}
|
||||
|
||||
public HttpRequest(String method, String path, Map<String, String> headers, OutputStream out) {
|
||||
this.method = method;
|
||||
this.path = path;
|
||||
this.headers = headers;
|
||||
this.out = out;
|
||||
}
|
||||
|
||||
// We dont need no http library
|
||||
public static HttpRequest read(Socket socket) {
|
||||
try {
|
||||
var str = socket.getInputStream();
|
||||
var lines = new BufferedReader(new InputStreamReader(str));
|
||||
var line = lines.readLine();
|
||||
var i1 = line.indexOf(" ");
|
||||
var i2 = line.indexOf(" ", i1 + 1);
|
||||
|
||||
if (i1 < 0 || i2 < 0) {
|
||||
socket.close();
|
||||
return null;
|
||||
}
|
||||
|
||||
var method = line.substring(0, i1).trim().toUpperCase();
|
||||
var path = line.substring(i1 + 1, i2).trim();
|
||||
var headers = new HashMap<String, String>();
|
||||
|
||||
while (!(line = lines.readLine()).isEmpty()) {
|
||||
var i = line.indexOf(":");
|
||||
if (i < 0) continue;
|
||||
var name = line.substring(0, i).trim().toLowerCase();
|
||||
var value = line.substring(i + 1).trim();
|
||||
|
||||
if (name.length() == 0) continue;
|
||||
headers.put(name, value);
|
||||
}
|
||||
|
||||
if (headers.containsKey("content-length")) {
|
||||
try {
|
||||
var i = Integer.parseInt(headers.get("content-length"));
|
||||
str.skip(i);
|
||||
}
|
||||
catch (IllegalFormatException e) { /* ¯\_(ツ)_/¯ */ }
|
||||
}
|
||||
|
||||
return new HttpRequest(method, path, headers, socket.getOutputStream());
|
||||
}
|
||||
catch (IOException | NullPointerException e) { return null; }
|
||||
}
|
||||
}
|
||||
|
@ -1,19 +0,0 @@
|
||||
package me.topchetoeu.j2s.lib.debug;
|
||||
|
||||
import me.topchetoeu.j2s.compilation.json.JSON;
|
||||
import me.topchetoeu.j2s.compilation.json.JSONMap;
|
||||
|
||||
public class V8Error {
|
||||
public final String message;
|
||||
|
||||
public V8Error(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return JSON.stringify(new JSONMap().set("error", new JSONMap()
|
||||
.set("message", message)
|
||||
));
|
||||
}
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
package me.topchetoeu.j2s.lib.debug;
|
||||
|
||||
import me.topchetoeu.j2s.compilation.json.JSON;
|
||||
import me.topchetoeu.j2s.compilation.json.JSONMap;
|
||||
|
||||
public class V8Event {
|
||||
public final String name;
|
||||
public final JSONMap params;
|
||||
|
||||
public V8Event(String name, JSONMap params) {
|
||||
this.name = name;
|
||||
this.params = params;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return JSON.stringify(new JSONMap()
|
||||
.set("method", name)
|
||||
.set("params", params)
|
||||
);
|
||||
}
|
||||
}
|
@ -1,50 +0,0 @@
|
||||
package me.topchetoeu.j2s.lib.debug;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import me.topchetoeu.j2s.compilation.json.JSON;
|
||||
import me.topchetoeu.j2s.compilation.json.JSONElement;
|
||||
import me.topchetoeu.j2s.compilation.json.JSONMap;
|
||||
|
||||
public class V8Message {
|
||||
public final String name;
|
||||
public final int id;
|
||||
public final JSONMap params;
|
||||
|
||||
public V8Message(String name, int id, Map<String, JSONElement> params) {
|
||||
this.name = name;
|
||||
this.params = new JSONMap(params);
|
||||
this.id = id;
|
||||
}
|
||||
public V8Result respond(JSONMap result) {
|
||||
return new V8Result(id, result);
|
||||
}
|
||||
public V8Result respond() {
|
||||
return new V8Result(id, new JSONMap());
|
||||
}
|
||||
|
||||
public V8Message(JSONMap raw) {
|
||||
if (!raw.isNumber("id")) throw new IllegalArgumentException("Expected number property 'id'.");
|
||||
if (!raw.isString("method")) throw new IllegalArgumentException("Expected string property 'method'.");
|
||||
|
||||
this.name = raw.string("method");
|
||||
this.id = (int)raw.number("id");
|
||||
this.params = raw.contains("params") ? raw.map("params") : new JSONMap();
|
||||
}
|
||||
public V8Message(String raw) {
|
||||
this(JSON.parse(null, raw).map());
|
||||
}
|
||||
|
||||
public JSONMap toMap() {
|
||||
var res = new JSONMap();
|
||||
return res;
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return JSON.stringify(new JSONMap()
|
||||
.set("method", name)
|
||||
.set("params", params)
|
||||
.set("id", id)
|
||||
);
|
||||
}
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
package me.topchetoeu.j2s.lib.debug;
|
||||
|
||||
import me.topchetoeu.j2s.compilation.json.JSON;
|
||||
import me.topchetoeu.j2s.compilation.json.JSONMap;
|
||||
|
||||
public class V8Result {
|
||||
public final int id;
|
||||
public final JSONMap result;
|
||||
|
||||
public V8Result(int id, JSONMap result) {
|
||||
this.id = id;
|
||||
this.result = result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return JSON.stringify(new JSONMap()
|
||||
.set("id", id)
|
||||
.set("result", result)
|
||||
);
|
||||
}
|
||||
}
|
@ -1,186 +0,0 @@
|
||||
package me.topchetoeu.j2s.lib.debug;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.Socket;
|
||||
|
||||
import me.topchetoeu.j2s.lib.debug.WebSocketMessage.Type;
|
||||
|
||||
public class WebSocket implements AutoCloseable {
|
||||
public long maxLength = 1 << 20;
|
||||
|
||||
private Socket socket;
|
||||
private boolean closed = false;
|
||||
|
||||
private OutputStream out() throws IOException {
|
||||
return socket.getOutputStream();
|
||||
}
|
||||
private InputStream in() throws IOException {
|
||||
return socket.getInputStream();
|
||||
}
|
||||
|
||||
private long readLen(int byteLen) throws IOException {
|
||||
long res = 0;
|
||||
|
||||
if (byteLen == 126) {
|
||||
res |= in().read() << 8;
|
||||
res |= in().read();
|
||||
return res;
|
||||
}
|
||||
else if (byteLen == 127) {
|
||||
res |= in().read() << 56;
|
||||
res |= in().read() << 48;
|
||||
res |= in().read() << 40;
|
||||
res |= in().read() << 32;
|
||||
res |= in().read() << 24;
|
||||
res |= in().read() << 16;
|
||||
res |= in().read() << 8;
|
||||
res |= in().read();
|
||||
return res;
|
||||
}
|
||||
else return byteLen;
|
||||
}
|
||||
private byte[] readMask(boolean has) throws IOException {
|
||||
if (has) {
|
||||
return new byte[] {
|
||||
(byte)in().read(),
|
||||
(byte)in().read(),
|
||||
(byte)in().read(),
|
||||
(byte)in().read()
|
||||
};
|
||||
}
|
||||
else return new byte[4];
|
||||
}
|
||||
|
||||
private void writeLength(int len) throws IOException {
|
||||
if (len < 126) {
|
||||
out().write((int)len);
|
||||
}
|
||||
else if (len <= 0xFFFF) {
|
||||
out().write(126);
|
||||
out().write((int)(len >> 8) & 0xFF);
|
||||
out().write((int)len & 0xFF);
|
||||
}
|
||||
else {
|
||||
out().write(127);
|
||||
out().write(0);
|
||||
out().write(0);
|
||||
out().write(0);
|
||||
out().write(0);
|
||||
out().write((len >> 24) & 0xFF);
|
||||
out().write((len >> 16) & 0xFF);
|
||||
out().write((len >> 8) & 0xFF);
|
||||
out().write(len & 0xFF);
|
||||
}
|
||||
}
|
||||
private synchronized void write(int type, byte[] data) throws IOException {
|
||||
out().write(type | 0x80);
|
||||
writeLength(data.length);
|
||||
out().write(data);
|
||||
}
|
||||
|
||||
public void send(String data) throws IOException {
|
||||
if (closed) throw new IllegalStateException("Websocket is closed.");
|
||||
write(1, data.getBytes());
|
||||
}
|
||||
public void send(byte[] data) throws IOException {
|
||||
if (closed) throw new IllegalStateException("Websocket is closed.");
|
||||
write(2, data);
|
||||
}
|
||||
public void send(WebSocketMessage msg) throws IOException {
|
||||
if (msg.type == Type.Binary) send(msg.binaryData());
|
||||
else send(msg.textData());
|
||||
}
|
||||
public void send(Object data) throws IOException {
|
||||
if (closed) throw new IllegalStateException("Websocket is closed.");
|
||||
write(1, data.toString().getBytes());
|
||||
}
|
||||
|
||||
public void close(String reason) {
|
||||
if (socket != null) {
|
||||
try {
|
||||
write(8, reason.getBytes());
|
||||
socket.close();
|
||||
}
|
||||
catch (Throwable e) { }
|
||||
}
|
||||
|
||||
socket = null;
|
||||
closed = true;
|
||||
}
|
||||
public void close() {
|
||||
close("");
|
||||
}
|
||||
|
||||
private WebSocketMessage fail(String reason) {
|
||||
System.out.println("WebSocket Error: " + reason);
|
||||
close(reason);
|
||||
return null;
|
||||
}
|
||||
|
||||
private byte[] readData() throws IOException {
|
||||
var maskLen = in().read();
|
||||
var hasMask = (maskLen & 0x80) != 0;
|
||||
var len = (int)readLen(maskLen & 0x7F);
|
||||
var mask = readMask(hasMask);
|
||||
|
||||
if (len > maxLength) fail("WebSocket Error: client exceeded configured max message size");
|
||||
else {
|
||||
var buff = new byte[len];
|
||||
|
||||
if (in().read(buff) < len) fail("WebSocket Error: payload too short");
|
||||
else {
|
||||
for (int i = 0; i < len; i++) {
|
||||
buff[i] ^= mask[(int)(i % 4)];
|
||||
}
|
||||
return buff;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public WebSocketMessage receive() throws IOException {
|
||||
var data = new ByteArrayOutputStream();
|
||||
var type = 0;
|
||||
|
||||
while (socket != null && !closed) {
|
||||
var finId = in().read();
|
||||
if (finId < 0) break;
|
||||
var fin = (finId & 0x80) != 0;
|
||||
int id = finId & 0x0F;
|
||||
|
||||
if (id == 0x8) { close(); return null; }
|
||||
if (id >= 0x8) {
|
||||
if (!fin) return fail("WebSocket Error: client-sent control frame was fragmented");
|
||||
if (id == 0x9) write(0xA, data.toByteArray());
|
||||
continue;
|
||||
}
|
||||
|
||||
if (type == 0) type = id;
|
||||
if (type == 0) return fail("WebSocket Error: client used opcode 0x00 for first fragment");
|
||||
|
||||
var buff = readData();
|
||||
if (buff == null) break;
|
||||
|
||||
if (data.size() + buff.length > maxLength) return fail("WebSocket Error: client exceeded configured max message size");
|
||||
data.write(buff);
|
||||
|
||||
if (!fin) continue;
|
||||
var raw = data.toByteArray();
|
||||
|
||||
if (type == 1) {
|
||||
return new WebSocketMessage(new String(raw));
|
||||
}
|
||||
else return new WebSocketMessage(raw);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public WebSocket(Socket socket) {
|
||||
this.socket = socket;
|
||||
}
|
||||
}
|
@ -1,29 +0,0 @@
|
||||
package me.topchetoeu.j2s.lib.debug;
|
||||
|
||||
public class WebSocketMessage {
|
||||
public static enum Type {
|
||||
Text,
|
||||
Binary,
|
||||
}
|
||||
|
||||
public final Type type;
|
||||
private final Object data;
|
||||
|
||||
public final String textData() {
|
||||
if (type != Type.Text) throw new IllegalStateException("Message is not text.");
|
||||
return (String)data;
|
||||
}
|
||||
public final byte[] binaryData() {
|
||||
if (type != Type.Binary) throw new IllegalStateException("Message is not binary.");
|
||||
return (byte[])data;
|
||||
}
|
||||
|
||||
public WebSocketMessage(String data) {
|
||||
this.type = Type.Text;
|
||||
this.data = data;
|
||||
}
|
||||
public WebSocketMessage(byte[] data) {
|
||||
this.type = Type.Binary;
|
||||
this.data = data;
|
||||
}
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
export default function _classPrivateFieldLooseBase(obj) {
|
||||
return obj;
|
||||
}
|
@ -1,13 +0,0 @@
|
||||
import { print, self, symbol } from "../stdlib/primordials.ts";
|
||||
import { Object } from "../stdlib/values/object.ts";
|
||||
|
||||
self.Object = {
|
||||
defineProperty: function (obj, key, desc) {
|
||||
if (obj == null) return obj;
|
||||
Object.defineProperty(obj, key, desc);
|
||||
}
|
||||
};
|
||||
|
||||
export default function _classPrivateFieldLooseKey(key) {
|
||||
return symbol.makeSymbol(key);
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
import { func, object, print } from "../stdlib/primordials.ts";
|
||||
import { object } from "../stdlib/primordials.ts";
|
||||
|
||||
function _defineProperties(target, arr) {
|
||||
if (!arr) return;
|
||||
@ -31,8 +31,5 @@ export default function _createClass(clazz, instance, nonInstance) {
|
||||
_defineProperties(clazz.prototype, instance);
|
||||
_defineProperties(clazz, nonInstance);
|
||||
|
||||
func.setCallable(clazz, false);
|
||||
func.setConstructable(clazz, true);
|
||||
|
||||
return clazz;
|
||||
}
|
@ -1,28 +1,25 @@
|
||||
import { buffer, type InternalBuffer, map, symbol } from "../primordials.ts";
|
||||
|
||||
export const abs = new map(true);
|
||||
export const abKey: unique symbol = symbol.getSymbol("ArrayBuffer.impl") as any;
|
||||
|
||||
export class ArrayBuffer {
|
||||
#internal!: InternalBuffer;
|
||||
public [abKey]!: InternalBuffer;
|
||||
|
||||
public get byteLength() {
|
||||
return this.#internal.length;
|
||||
return this[abKey].length;
|
||||
}
|
||||
public get byteOffset() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public constructor(val: unknown) {
|
||||
if (buffer.isBuff(val)) this.#internal = val;
|
||||
else this.#internal = buffer.buff(Number(val));
|
||||
}
|
||||
|
||||
public static unwrap(instance: ArrayBuffer) {
|
||||
return instance.#internal;
|
||||
if (buffer.isBuff(val)) this[abKey] = val;
|
||||
else this[abKey] = buffer.buff(Number(val));
|
||||
}
|
||||
}
|
||||
|
||||
function wrapAB(buff: InternalBuffer): ArrayBuffer {
|
||||
export function getAB(buff: InternalBuffer): ArrayBuffer {
|
||||
let res = abs.get(buff);
|
||||
if (res == null) {
|
||||
res = new ArrayBuffer(buff);
|
||||
@ -31,11 +28,3 @@ function wrapAB(buff: InternalBuffer): ArrayBuffer {
|
||||
|
||||
return res;
|
||||
}
|
||||
const unwrapAB = ArrayBuffer.unwrap;
|
||||
|
||||
delete (ArrayBuffer as any).unwrap;
|
||||
|
||||
|
||||
|
||||
export { wrapAB, unwrapAB };
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { buffer } from "../primordials.ts";
|
||||
import { token, TypedArray, typedArrayFuncs } from "./TypedArray.ts";
|
||||
import { abstractIgnore, TypedArray, typedArrayFuncs } from "./TypedArray.ts";
|
||||
|
||||
const factory = buffer.int32;
|
||||
const funcs = typedArrayFuncs(4, factory);
|
||||
@ -19,7 +19,7 @@ export class Int32Array extends TypedArray {
|
||||
}
|
||||
|
||||
public constructor(obj: any, start?: number, end?: number) {
|
||||
super(token);
|
||||
super(abstractIgnore);
|
||||
return funcs.construct(obj, start, end) as any;
|
||||
}
|
||||
}
|
@ -1,9 +1,9 @@
|
||||
import { buffer, func, type InternalBuffer, object, string, symbol } from "../primordials.ts";
|
||||
import { symbols, wrapI } from "../utils.ts";
|
||||
import { Error, TypeError } from "../values/errors.ts";
|
||||
import { ArrayBuffer, unwrapAB, wrapAB } from "./ArrayBuffer.ts";
|
||||
import { abKey, ArrayBuffer, getAB } from "./ArrayBuffer.ts";
|
||||
|
||||
export const token = symbol.getSymbol("TypedArray.abstractIgnore");
|
||||
export const abstractIgnore = symbol.getSymbol("TypedArray.abstractIgnore");
|
||||
|
||||
export function typedArrayFuncs(perEl: number, constructor: (buff: InternalBuffer, start: number, end: number) => number[]) {
|
||||
return {
|
||||
@ -56,7 +56,7 @@ export function typedArrayFuncs(perEl: number, constructor: (buff: InternalBuffe
|
||||
return constructor(buffer.buff(self * perEl), 0, self);
|
||||
}
|
||||
if (self instanceof ArrayBuffer) {
|
||||
const internal = unwrapAB(self);
|
||||
const internal = self[abKey];
|
||||
if (start === undefined) start = 0;
|
||||
if (end === undefined) end = (internal.length / perEl) | 0;
|
||||
return constructor(internal, start, end);
|
||||
@ -90,7 +90,7 @@ export function typedArrayFuncs(perEl: number, constructor: (buff: InternalBuffe
|
||||
|
||||
export class TypedArray {
|
||||
public get buffer() {
|
||||
return wrapAB(buffer.backer(this as any));
|
||||
return getAB(buffer.backer(this as any));
|
||||
}
|
||||
public get byteOffset(): number {
|
||||
throw new Error("abstract");
|
||||
@ -212,8 +212,8 @@ export class TypedArray {
|
||||
return this;
|
||||
}
|
||||
|
||||
public constructor(_token?: typeof token) {
|
||||
if (_token !== token) {
|
||||
public constructor(token?: typeof abstractIgnore) {
|
||||
if (token !== abstractIgnore) {
|
||||
throw new TypeError("TypedArray constructor can't be called");
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { buffer } from "../primordials.ts";
|
||||
import { token, TypedArray, typedArrayFuncs } from "./TypedArray.ts";
|
||||
import { abstractIgnore, TypedArray, typedArrayFuncs } from "./TypedArray.ts";
|
||||
|
||||
const factory = buffer.uint8;
|
||||
const funcs = typedArrayFuncs(1, factory);
|
||||
@ -26,7 +26,7 @@ export class Uint8Array extends TypedArray {
|
||||
}
|
||||
|
||||
public constructor(obj: any, start?: number, end?: number) {
|
||||
super(token);
|
||||
super(abstractIgnore);
|
||||
return funcs.construct(obj, start, end) as any;
|
||||
}
|
||||
}
|
@ -1,11 +1,13 @@
|
||||
import { now, number, symbol } from "../primordials.ts";
|
||||
import { now, symbol } from "../primordials.ts";
|
||||
|
||||
const timeKey: unique symbol = symbol.makeSymbol("") as any;
|
||||
|
||||
export const Date = (() => {
|
||||
class Date {
|
||||
#time: number;
|
||||
[timeKey]!: number;
|
||||
|
||||
public constructor() {
|
||||
this.#time = number.NaN;
|
||||
|
||||
}
|
||||
|
||||
public static now() {
|
||||
|
@ -1,49 +1,51 @@
|
||||
import { Array } from "../values/array.ts";
|
||||
import { func, map } from "../primordials.ts";
|
||||
import { func, map, symbol } from "../primordials.ts";
|
||||
import { symbols } from "../utils.ts";
|
||||
|
||||
const mapKey: unique symbol = symbol.makeSymbol("Map.impl") as any;
|
||||
|
||||
export class Map<K, V> {
|
||||
#map: InstanceType<typeof map>;
|
||||
private [mapKey]: InstanceType<typeof map>;
|
||||
|
||||
public get size() {
|
||||
return this.#map.size();
|
||||
return this[mapKey].size();
|
||||
}
|
||||
|
||||
public get(key: K): V {
|
||||
return this.#map.get(key);
|
||||
return this[mapKey].get(key);
|
||||
}
|
||||
public has(key: K): boolean {
|
||||
return this.#map.has(key);
|
||||
return this[mapKey].has(key);
|
||||
}
|
||||
public set(key: K, val: V) {
|
||||
this.#map.set(key, val);
|
||||
this[mapKey].set(key, val);
|
||||
return this;
|
||||
}
|
||||
public delete(key: K): boolean {
|
||||
if (!this.#map.has(key)) return false;
|
||||
if (!this[mapKey].has(key)) return false;
|
||||
else {
|
||||
this.#map.delete(key);
|
||||
this[mapKey].delete(key);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
public clear() {
|
||||
this.#map.clear();
|
||||
this[mapKey].clear();
|
||||
}
|
||||
|
||||
public keys(): K[] {
|
||||
return this.#map.keys();
|
||||
return this[mapKey].keys();
|
||||
}
|
||||
public values(): V[] {
|
||||
const res = this.#map.keys();
|
||||
const res = this[mapKey].keys();
|
||||
for (let i = 0; i < res.length; i++) {
|
||||
res[i] = this.#map.get(res[i]);
|
||||
res[i] = this[mapKey].get(res[i]);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
public entries(): [K, V][] {
|
||||
const res = this.#map.keys();
|
||||
const res = this[mapKey].keys();
|
||||
for (let i = 0; i < res.length; i++) {
|
||||
res[i] = [res[i], this.#map.get(res[i])];
|
||||
res[i] = [res[i], this[mapKey].get(res[i])];
|
||||
}
|
||||
return res;
|
||||
}
|
||||
@ -60,7 +62,7 @@ export class Map<K, V> {
|
||||
}
|
||||
|
||||
public constructor(iterable?: Iterable<[K, V]>) {
|
||||
const _map = this.#map = new map();
|
||||
const _map = this[mapKey] = new map();
|
||||
|
||||
if (iterable != null) {
|
||||
if (Array.isArray(iterable)) {
|
||||
@ -79,31 +81,31 @@ export class Map<K, V> {
|
||||
}
|
||||
}
|
||||
export class WeakMap<K, V> {
|
||||
#map: InstanceType<typeof map>;
|
||||
private [mapKey]: InstanceType<typeof map>;
|
||||
|
||||
public get(key: K): V {
|
||||
return this.#map.get(key);
|
||||
return this[mapKey].get(key);
|
||||
}
|
||||
public has(key: K): boolean {
|
||||
return this.#map.has(key);
|
||||
return this[mapKey].has(key);
|
||||
}
|
||||
public set(key: K, val: V) {
|
||||
this.#map.set(key, val);
|
||||
this[mapKey].set(key, val);
|
||||
return this;
|
||||
}
|
||||
public delete(key: K): boolean {
|
||||
if (!this.#map.has(key)) return false;
|
||||
if (!this[mapKey].has(key)) return false;
|
||||
else {
|
||||
this.#map.delete(key);
|
||||
this[mapKey].delete(key);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
public clear() {
|
||||
this.#map.clear();
|
||||
this[mapKey].clear();
|
||||
}
|
||||
|
||||
public constructor(iterable?: Iterable<[K, V]>) {
|
||||
const _map = this.#map = new map(true);
|
||||
const _map = this[mapKey] = new map(true);
|
||||
|
||||
if (iterable != null) {
|
||||
if (Array.isArray(iterable)) {
|
||||
@ -121,3 +123,6 @@ export class WeakMap<K, V> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func.setCallable(Map, false);
|
||||
func.setCallable(WeakMap, false);
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { next } from "../primordials.ts";
|
||||
import { func, next, object, symbol } from "../primordials.ts";
|
||||
|
||||
enum PromiseState {
|
||||
Pending = "pend",
|
||||
@ -6,106 +6,109 @@ enum PromiseState {
|
||||
Rejected = "rej",
|
||||
}
|
||||
|
||||
const pState: unique symbol = symbol.makeSymbol("Promise.state") as any;
|
||||
const pValue: unique symbol = symbol.makeSymbol("Promise.value") as any;
|
||||
const pFulHandles: unique symbol = symbol.makeSymbol("Promise.fulfillHandles") as any;
|
||||
const pRejHandles: unique symbol = symbol.makeSymbol("Promise.rejectHandles") as any;
|
||||
|
||||
function makePromise<T>(): Promise<T> {
|
||||
return object.setPrototype({
|
||||
[pState]: PromiseState.Pending,
|
||||
[pFulHandles]: [],
|
||||
[pRejHandles]: [],
|
||||
}, Promise.prototype) as Promise<T>;
|
||||
}
|
||||
|
||||
function fulfill(self: Promise<any>, val: any) {
|
||||
if (self[pState] !== PromiseState.Pending) return;
|
||||
if (self === val) throw new Error("A promise may not be fulfilled with itself");
|
||||
|
||||
if (val != null && typeof val.then === "function") {
|
||||
val.then(
|
||||
(val: any) => fulfill(self, val),
|
||||
(err: any) => reject(self, err),
|
||||
);
|
||||
}
|
||||
else {
|
||||
self[pValue] = val;
|
||||
self[pState] = PromiseState.Fulfilled;
|
||||
|
||||
const handles = self[pFulHandles]!;
|
||||
|
||||
for (let i = 0; i < handles.length; i++) {
|
||||
handles[i](val);
|
||||
}
|
||||
|
||||
self[pFulHandles] = undefined;
|
||||
self[pRejHandles] = undefined;
|
||||
}
|
||||
}
|
||||
function reject(self: Promise<any>, val: any) {
|
||||
if (self[pState] !== PromiseState.Pending) return;
|
||||
if (self === val) throw new Error("A promise may not be rejected with itself");
|
||||
|
||||
if (val != null && typeof val.then === "function") {
|
||||
val.then(
|
||||
(val: any) => reject(self, val),
|
||||
(err: any) => reject(self, err),
|
||||
);
|
||||
}
|
||||
else {
|
||||
self[pValue] = val;
|
||||
self[pState] = PromiseState.Rejected;
|
||||
|
||||
const handles = self[pRejHandles]!;
|
||||
|
||||
for (let i = 0; i < handles.length; i++) {
|
||||
handles[i](val);
|
||||
}
|
||||
|
||||
self[pFulHandles] = undefined;
|
||||
self[pRejHandles] = undefined;
|
||||
}
|
||||
}
|
||||
function handle<T>(self: Promise<T>, ful?: (val: T) => void, rej?: (err: any) => void) {
|
||||
if (self[pState] === PromiseState.Pending) {
|
||||
if (ful != null) {
|
||||
self[pFulHandles]![self[pFulHandles]!.length] = ful;
|
||||
}
|
||||
if (rej != null) {
|
||||
self[pRejHandles]![self[pRejHandles]!.length] = rej;
|
||||
}
|
||||
}
|
||||
else if (self[pState] === PromiseState.Fulfilled) {
|
||||
if (ful != null) ful(self[pValue] as T);
|
||||
}
|
||||
else if (self[pState] === PromiseState.Rejected) {
|
||||
if (rej != null) rej(self[pValue]);
|
||||
}
|
||||
}
|
||||
|
||||
export class Promise<T> {
|
||||
static #InternalPromise = function <T>(this: Promise<T>) {
|
||||
this.#state = PromiseState.Pending;
|
||||
this.#fulHandles = [];
|
||||
this.#rejHandles = [];
|
||||
} as any as new <T>() => Promise<T>;
|
||||
|
||||
static {
|
||||
this.#InternalPromise.prototype = this.prototype;
|
||||
}
|
||||
|
||||
#state: PromiseState;
|
||||
#value?: T | unknown;
|
||||
#fulHandles?: ((val: T) => void)[] = [];
|
||||
#rejHandles?: ((val: T) => void)[] = [];
|
||||
|
||||
#fulfill(val: any) {
|
||||
if (this.#state !== PromiseState.Pending) return;
|
||||
if (this === val) throw new Error("A promise may not be fulfilled with itself");
|
||||
|
||||
if (val != null && typeof val.then === "function") {
|
||||
val.then(
|
||||
(val: any) => this.#fulfill(val),
|
||||
(err: any) => this.#reject(err),
|
||||
);
|
||||
}
|
||||
else {
|
||||
this.#value = val;
|
||||
this.#state = PromiseState.Fulfilled;
|
||||
|
||||
const handles = this.#fulHandles!;
|
||||
|
||||
for (let i = 0; i < handles.length; i++) {
|
||||
handles[i](val);
|
||||
}
|
||||
|
||||
this.#fulHandles = undefined;
|
||||
this.#rejHandles = undefined;
|
||||
}
|
||||
}
|
||||
#reject(val: any) {
|
||||
if (this.#state !== PromiseState.Pending) return;
|
||||
if (this === val) throw new Error("A promise may not be rejected with itself");
|
||||
|
||||
if (val != null && typeof val.then === "function") {
|
||||
val.then(
|
||||
(val: any) => this.#reject(val),
|
||||
(err: any) => this.#reject(err),
|
||||
);
|
||||
}
|
||||
else {
|
||||
this.#value = val;
|
||||
this.#state = PromiseState.Rejected;
|
||||
|
||||
const handles = this.#rejHandles!;
|
||||
|
||||
for (let i = 0; i < handles.length; i++) {
|
||||
handles[i](val);
|
||||
}
|
||||
|
||||
this.#fulHandles = undefined;
|
||||
this.#rejHandles = undefined;
|
||||
}
|
||||
}
|
||||
#handle(ful?: (val: T) => void, rej?: (err: any) => void) {
|
||||
if (this.#state === PromiseState.Pending) {
|
||||
if (ful != null) {
|
||||
this.#fulHandles![this.#fulHandles!.length] = ful;
|
||||
}
|
||||
if (rej != null) {
|
||||
this.#rejHandles![this.#rejHandles!.length] = rej;
|
||||
}
|
||||
}
|
||||
else if (this.#state === PromiseState.Fulfilled) {
|
||||
if (ful != null) ful(this.#value as T);
|
||||
}
|
||||
else if (this.#state === PromiseState.Rejected) {
|
||||
if (rej != null) rej(this.#value);
|
||||
}
|
||||
}
|
||||
public [pState]: PromiseState;
|
||||
public [pValue]?: T | unknown;
|
||||
public [pFulHandles]?: ((val: T) => void)[] = [];
|
||||
public [pRejHandles]?: ((val: T) => void)[] = [];
|
||||
|
||||
public then<Res>(ful?: (val: T) => Res, rej?: (err: any) => Res) {
|
||||
if (typeof ful !== "function") ful = undefined;
|
||||
if (typeof rej !== "function") rej = undefined;
|
||||
|
||||
const promise = new Promise.#InternalPromise<Res>();
|
||||
const promise = makePromise<Res>();
|
||||
|
||||
this.#handle(
|
||||
handle(this,
|
||||
val => next(() => {
|
||||
if (ful == null) promise.#fulfill(val);
|
||||
if (ful == null) fulfill(promise, val);
|
||||
else {
|
||||
try { promise.#fulfill(ful(val)); }
|
||||
catch (e) { promise.#reject(e); }
|
||||
try { fulfill(promise, ful(val)); }
|
||||
catch (e) { reject(promise, e); }
|
||||
}
|
||||
}),
|
||||
err => next(() => {
|
||||
if (rej == null) promise.#reject(err);
|
||||
if (rej == null) reject(promise, err);
|
||||
else {
|
||||
try { promise.#fulfill(rej(err)); }
|
||||
catch (e) { promise.#reject(e); }
|
||||
try { fulfill(promise, rej(err)); }
|
||||
catch (e) { reject(promise, e); }
|
||||
}
|
||||
}),
|
||||
);
|
||||
@ -131,19 +134,21 @@ export class Promise<T> {
|
||||
}
|
||||
|
||||
public constructor(fn: (fulfil: (val: T) => void, reject: (err: unknown) => void) => void) {
|
||||
this.#state = PromiseState.Pending;
|
||||
this[pState] = PromiseState.Pending;
|
||||
|
||||
fn(val => this.#fulfill(val), err => this.#reject(err));
|
||||
fn(val => fulfill(this, val), err => reject(this, err));
|
||||
}
|
||||
|
||||
public static resolve(val: any) {
|
||||
const res = new this.#InternalPromise();
|
||||
res.#fulfill(val);
|
||||
const res = makePromise();
|
||||
fulfill(res, val);
|
||||
return res;
|
||||
}
|
||||
public static reject(val: any) {
|
||||
const res = new this.#InternalPromise();
|
||||
res.#reject(val);
|
||||
const res = makePromise();
|
||||
reject(res, val);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
func.setCallable(Promise, false);
|
||||
|
@ -1,40 +1,42 @@
|
||||
import { Array } from "../values/array.ts";
|
||||
import { func, map } from "../primordials.ts";
|
||||
import { func, map, symbol } from "../primordials.ts";
|
||||
import { symbols } from "../utils.ts";
|
||||
|
||||
const mapKey: unique symbol = symbol.makeSymbol("Set.impl") as any;
|
||||
|
||||
export class Set<T> {
|
||||
#map: InstanceType<typeof map>;
|
||||
private [mapKey]: InstanceType<typeof map>;
|
||||
|
||||
public get size() {
|
||||
return this.#map.size();
|
||||
return this[mapKey].size();
|
||||
}
|
||||
|
||||
public has(key: T): boolean {
|
||||
return this.#map.has(key);
|
||||
return this[mapKey].has(key);
|
||||
}
|
||||
public add(val: T) {
|
||||
this.#map.set(val, true);
|
||||
this[mapKey].set(val, true);
|
||||
return this;
|
||||
}
|
||||
public delete(val: T): boolean {
|
||||
if (!this.#map.has(val)) return false;
|
||||
if (!this[mapKey].has(val)) return false;
|
||||
else {
|
||||
this.#map.delete(val);
|
||||
this[mapKey].delete(val);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
public clear() {
|
||||
this.#map.clear();
|
||||
this[mapKey].clear();
|
||||
}
|
||||
|
||||
public keys(): T[] {
|
||||
return this.#map.keys();
|
||||
return this[mapKey].keys();
|
||||
}
|
||||
public values(): T[] {
|
||||
return this.#map.keys();
|
||||
return this[mapKey].keys();
|
||||
}
|
||||
public entries(): [T, T][] {
|
||||
const res = this.#map.keys();
|
||||
const res = this[mapKey].keys();
|
||||
|
||||
for (let i = 0; i < res.length; i++) {
|
||||
res[i] = [res[i], res[i]];
|
||||
@ -55,7 +57,7 @@ export class Set<T> {
|
||||
}
|
||||
|
||||
public constructor(iterable?: Iterable<T>) {
|
||||
const _map = this.#map = new map();
|
||||
const _map = this[mapKey] = new map();
|
||||
|
||||
if (iterable != null) {
|
||||
if (Array.isArray(iterable)) {
|
||||
@ -75,28 +77,28 @@ export class Set<T> {
|
||||
}
|
||||
|
||||
export class WeakSet<T> {
|
||||
#map: InstanceType<typeof map>;
|
||||
private [mapKey]: InstanceType<typeof map>;
|
||||
|
||||
public has(key: T): boolean {
|
||||
return this.#map.has(key);
|
||||
return this[mapKey].has(key);
|
||||
}
|
||||
public add(val: T) {
|
||||
this.#map.set(val, true);
|
||||
this[mapKey].set(val, true);
|
||||
return this;
|
||||
}
|
||||
public delete(val: T): boolean {
|
||||
if (!this.#map.has(val)) return false;
|
||||
if (!this[mapKey].has(val)) return false;
|
||||
else {
|
||||
this.#map.delete(val);
|
||||
this[mapKey].delete(val);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
public clear() {
|
||||
this.#map.clear();
|
||||
this[mapKey].clear();
|
||||
}
|
||||
|
||||
public constructor(iterable?: Iterable<T>) {
|
||||
const _map = this.#map = new map(true);
|
||||
const _map = this[mapKey] = new map(true);
|
||||
|
||||
if (iterable != null) {
|
||||
if (Array.isArray(iterable)) {
|
||||
@ -114,3 +116,6 @@ export class WeakSet<T> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func.setCallable(Set, false);
|
||||
func.setCallable(WeakSet, false);
|
||||
|
@ -4,14 +4,6 @@ export interface InternalBuffer {
|
||||
length: number;
|
||||
[buffSymbol]: "buffer";
|
||||
}
|
||||
export interface InternalSocket {
|
||||
read(onRes: (data: Uint8Array) => void, onErr: (err: unknown) => void, onDone: () => void): void;
|
||||
write(data: Uint8Array, onRes: () => void, onErr: (err: unknown) => void): void;
|
||||
}
|
||||
export interface InternalServer {
|
||||
bind(address: string, onRes: () => void, onErr: (err: unknown) => void): void;
|
||||
next(onRes: (socket: InternalSocket) => void, onErr: (err: unknown) => void, onDone: () => void): void;
|
||||
}
|
||||
|
||||
export interface SymbolPrimordials {
|
||||
makeSymbol(name: string): symbol;
|
||||
@ -90,9 +82,6 @@ export interface JSONPrimordials {
|
||||
parse(data: string): any;
|
||||
stringify(data: any): string;
|
||||
}
|
||||
export interface NetPrimordials {
|
||||
server(): InternalServer;
|
||||
}
|
||||
|
||||
export interface Primordials {
|
||||
symbol: SymbolPrimordials;
|
||||
@ -102,7 +91,6 @@ export interface Primordials {
|
||||
function: FunctionPrimordials;
|
||||
json: JSONPrimordials;
|
||||
buffer: BufferPrimordials;
|
||||
net: NetPrimordials;
|
||||
map: new (weak?: boolean) => {
|
||||
get(key: any): any;
|
||||
has(key: any): boolean;
|
||||
@ -139,7 +127,6 @@ export const {
|
||||
buffer,
|
||||
function: func,
|
||||
json,
|
||||
net: socket,
|
||||
map,
|
||||
regex,
|
||||
setGlobalPrototypes,
|
||||
@ -151,5 +138,4 @@ export const {
|
||||
print,
|
||||
} = primordials;
|
||||
|
||||
export type regex = InstanceType<typeof regex>;
|
||||
export const self = (globalThis as any);
|
||||
export type regex = InstanceType<typeof regex>;
|
@ -1,63 +0,0 @@
|
||||
import { Promise as Promise } from "./classes/promise";
|
||||
import { func, InternalServer, InternalSocket, symbol } from "./primordials";
|
||||
import { Error } from "./values/errors";
|
||||
|
||||
const socketToken = symbol.makeSymbol("ServerSocket.token");
|
||||
|
||||
export class ServerSocket {
|
||||
#internal: InternalSocket;
|
||||
|
||||
public read() {
|
||||
return new Promise((ful, rej) => {
|
||||
this.#internal.read(ful, rej, ful as any);
|
||||
});
|
||||
}
|
||||
public write(data: Uint8Array) {
|
||||
return new Promise<void>((ful, rej) => {
|
||||
this.#internal.write(data, ful, rej);
|
||||
});
|
||||
}
|
||||
|
||||
public next() {
|
||||
return new Promise((ful, rej) => {
|
||||
this.#internal.read(
|
||||
data => ful({ value: data, done: false }),
|
||||
rej,
|
||||
() => ful({ value: undefined, done: true })
|
||||
);
|
||||
});
|
||||
}
|
||||
public [Symbol.iterator](): this {
|
||||
return this;
|
||||
}
|
||||
|
||||
public constructor(token: typeof socketToken, socket: InternalSocket) {
|
||||
if (token !== socketToken) throw new Error("Invalid token for creation");
|
||||
this.#internal = socket;
|
||||
}
|
||||
}
|
||||
|
||||
export class Server {
|
||||
#internal: InternalServer;
|
||||
|
||||
public bind(address: string) {
|
||||
return new Promise<void>((res, rej) => this.#internal.bind(address, res, rej));
|
||||
}
|
||||
|
||||
public next() {
|
||||
return new Promise((ful, rej) => {
|
||||
this.#internal.next(
|
||||
data => ful({ value: new ServerSocket(socketToken, data), done: false }),
|
||||
rej,
|
||||
() => ful({ value: undefined, done: true })
|
||||
);
|
||||
});
|
||||
}
|
||||
public [Symbol.iterator](): this {
|
||||
return this;
|
||||
}
|
||||
|
||||
public constructor(server: InternalServer) {
|
||||
this.#internal = server;
|
||||
}
|
||||
}
|
@ -1,12 +1,14 @@
|
||||
import { func, regex } from "../primordials.ts";
|
||||
import { func, regex, symbol } from "../primordials.ts";
|
||||
import { String } from "./string.ts";
|
||||
import { type ReplaceRange } from "../utils.ts";
|
||||
import { applyReplaces } from "../utils.ts";
|
||||
import { applySplits } from "../utils.ts";
|
||||
import { symbols } from "../utils.ts";
|
||||
|
||||
const regexKey: unique symbol = symbol.makeSymbol("RegExp.impl") as any;
|
||||
|
||||
export class RegExp {
|
||||
#regex!: InstanceType<typeof regex>;
|
||||
private [regexKey]!: InstanceType<typeof regex>;
|
||||
|
||||
public readonly source!: string;
|
||||
public readonly flags!: string;
|
||||
@ -68,14 +70,14 @@ export class RegExp {
|
||||
this.unicodeSets = unicodeSets;
|
||||
this.sticky = sticky;
|
||||
|
||||
this.#regex = new regex(source, multiline, ignoreCase, dotall, unicode, unicodeSets);
|
||||
this[regexKey] = new regex(source, multiline, ignoreCase, dotall, unicode, unicodeSets);
|
||||
}
|
||||
|
||||
public exec(target: string) {
|
||||
const useLast = this.global || this.sticky;
|
||||
const start = useLast ? this.lastIndex : 0;
|
||||
|
||||
const match = this.#regex.exec(target, start, this.indices);
|
||||
const match = this[regexKey].exec(target, start, this.indices);
|
||||
if (match != null && !(this.sticky && match.matches.index !== start)) {
|
||||
if (useLast) this.lastIndex = match.end;
|
||||
return match.matches;
|
||||
@ -90,7 +92,7 @@ export class RegExp {
|
||||
|
||||
public [symbols.split](target: string, limit?: number) {
|
||||
return applySplits(target, limit, offset => {
|
||||
const val = this.#regex.exec(target, offset, false);
|
||||
const val = this[regexKey].exec(target, offset, false);
|
||||
if (val == null) return undefined;
|
||||
|
||||
return { start: val.matches.index!, end: val.end };
|
||||
@ -98,7 +100,7 @@ export class RegExp {
|
||||
}
|
||||
public [symbols.replace](target: string, replacer: any) {
|
||||
const matches: ReplaceRange[] = [];
|
||||
const regex = this.#regex;
|
||||
const regex = this[regexKey];
|
||||
|
||||
if (this.global) {
|
||||
let offset = 0;
|
||||
@ -134,6 +136,3 @@ export class RegExp {
|
||||
return applyReplaces(target, matches, replacer, regex.groupCount() + 1);
|
||||
}
|
||||
}
|
||||
|
||||
func.setCallable(RegExp, true);
|
||||
func.setConstructable(RegExp, true);
|
||||
|
19
repl/src/main/java/me/topchetoeu/j2s/repl/V8Error.java
Normal file
19
repl/src/main/java/me/topchetoeu/j2s/repl/V8Error.java
Normal file
@ -0,0 +1,19 @@
|
||||
package me.topchetoeu.j2s.repl;
|
||||
|
||||
import me.topchetoeu.j2s.compilation.json.JSON;
|
||||
import me.topchetoeu.j2s.compilation.json.JSONMap;
|
||||
|
||||
public class V8Error {
|
||||
public final String message;
|
||||
|
||||
public V8Error(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return JSON.stringify(new JSONMap().set("error", new JSONMap()
|
||||
.set("message", message)
|
||||
));
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package me.topchetoeu.j2s.lib.debug;
|
||||
package me.topchetoeu.j2s.repl.debug;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.UncheckedIOException;
|
||||
@ -15,7 +15,7 @@ import me.topchetoeu.j2s.common.SyntaxException;
|
||||
import me.topchetoeu.j2s.compilation.json.JSON;
|
||||
import me.topchetoeu.j2s.compilation.json.JSONList;
|
||||
import me.topchetoeu.j2s.compilation.json.JSONMap;
|
||||
import me.topchetoeu.j2s.lib.debug.WebSocketMessage.Type;
|
||||
import me.topchetoeu.j2s.repl.debug.WebSocketMessage.Type;
|
||||
|
||||
public class DebugServer {
|
||||
public static String browserDisplayName = Metadata.name() + "/" + Metadata.version();
|
@ -0,0 +1,37 @@
|
||||
package me.topchetoeu.j2s.repl.debug;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import me.topchetoeu.j2s.runtime.debug.DebugHandler;
|
||||
|
||||
public interface Debugger extends DebugHandler {
|
||||
void close();
|
||||
|
||||
void enable(V8Message msg) throws IOException;
|
||||
void disable(V8Message msg) throws IOException;
|
||||
|
||||
void setBreakpointByUrl(V8Message msg) throws IOException;
|
||||
void removeBreakpoint(V8Message msg) throws IOException;
|
||||
void continueToLocation(V8Message msg) throws IOException;
|
||||
|
||||
void getScriptSource(V8Message msg) throws IOException;
|
||||
void getPossibleBreakpoints(V8Message msg) throws IOException;
|
||||
|
||||
void resume(V8Message msg) throws IOException;
|
||||
void pause(V8Message msg) throws IOException;
|
||||
|
||||
void stepInto(V8Message msg) throws IOException;
|
||||
void stepOut(V8Message msg) throws IOException;
|
||||
void stepOver(V8Message msg) throws IOException;
|
||||
|
||||
void setPauseOnExceptions(V8Message msg) throws IOException;
|
||||
|
||||
void evaluateOnCallFrame(V8Message msg) throws IOException;
|
||||
|
||||
void getProperties(V8Message msg) throws IOException;
|
||||
void releaseObjectGroup(V8Message msg) throws IOException;
|
||||
void releaseObject(V8Message msg) throws IOException;
|
||||
void callFunctionOn(V8Message msg) throws IOException;
|
||||
|
||||
void runtimeEnable(V8Message msg) throws IOException;
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
package me.topchetoeu.j2s.repl.debug;
|
||||
|
||||
public interface DebuggerProvider {
|
||||
Debugger getDebugger(WebSocket socket, HttpRequest req);
|
||||
}
|
101
repl/src/main/java/me/topchetoeu/j2s/repl/debug/HttpRequest.java
Normal file
101
repl/src/main/java/me/topchetoeu/j2s/repl/debug/HttpRequest.java
Normal file
@ -0,0 +1,101 @@
|
||||
package me.topchetoeu.j2s.repl.debug;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.net.Socket;
|
||||
import java.util.HashMap;
|
||||
import java.util.IllegalFormatException;
|
||||
import java.util.Map;
|
||||
|
||||
import me.topchetoeu.j2s.common.Reading;
|
||||
|
||||
public class HttpRequest {
|
||||
public final String method;
|
||||
public final String path;
|
||||
public final Map<String, String> headers;
|
||||
public final OutputStream out;
|
||||
|
||||
|
||||
public void writeCode(int code, String name) {
|
||||
try { out.write(("HTTP/1.1 " + code + " " + name + "\r\n").getBytes()); }
|
||||
catch (IOException e) { }
|
||||
}
|
||||
public void writeHeader(String name, String value) {
|
||||
try { out.write((name + ": " + value + "\r\n").getBytes()); }
|
||||
catch (IOException e) { }
|
||||
}
|
||||
public void writeLastHeader(String name, String value) {
|
||||
try { out.write((name + ": " + value + "\r\n\r\n").getBytes()); }
|
||||
catch (IOException e) { }
|
||||
}
|
||||
public void writeHeadersEnd() {
|
||||
try { out.write("\n".getBytes()); }
|
||||
catch (IOException e) { }
|
||||
}
|
||||
|
||||
public void writeResponse(int code, String name, String type, byte[] data) {
|
||||
writeCode(code, name);
|
||||
writeHeader("Content-Type", type);
|
||||
writeLastHeader("Content-Length", data.length + "");
|
||||
try {
|
||||
out.write(data);
|
||||
out.close();
|
||||
}
|
||||
catch (IOException e) { }
|
||||
}
|
||||
public void writeResponse(int code, String name, String type, InputStream data) {
|
||||
writeResponse(code, name, type, Reading.streamToBytes(data));
|
||||
}
|
||||
|
||||
public HttpRequest(String method, String path, Map<String, String> headers, OutputStream out) {
|
||||
this.method = method;
|
||||
this.path = path;
|
||||
this.headers = headers;
|
||||
this.out = out;
|
||||
}
|
||||
|
||||
// We dont need no http library
|
||||
public static HttpRequest read(Socket socket) {
|
||||
try {
|
||||
var str = socket.getInputStream();
|
||||
var lines = new BufferedReader(new InputStreamReader(str));
|
||||
var line = lines.readLine();
|
||||
var i1 = line.indexOf(" ");
|
||||
var i2 = line.indexOf(" ", i1 + 1);
|
||||
|
||||
if (i1 < 0 || i2 < 0) {
|
||||
socket.close();
|
||||
return null;
|
||||
}
|
||||
|
||||
var method = line.substring(0, i1).trim().toUpperCase();
|
||||
var path = line.substring(i1 + 1, i2).trim();
|
||||
var headers = new HashMap<String, String>();
|
||||
|
||||
while (!(line = lines.readLine()).isEmpty()) {
|
||||
var i = line.indexOf(":");
|
||||
if (i < 0) continue;
|
||||
var name = line.substring(0, i).trim().toLowerCase();
|
||||
var value = line.substring(i + 1).trim();
|
||||
|
||||
if (name.length() == 0) continue;
|
||||
headers.put(name, value);
|
||||
}
|
||||
|
||||
if (headers.containsKey("content-length")) {
|
||||
try {
|
||||
var i = Integer.parseInt(headers.get("content-length"));
|
||||
str.skip(i);
|
||||
}
|
||||
catch (IllegalFormatException e) { /* ¯\_(ツ)_/¯ */ }
|
||||
}
|
||||
|
||||
return new HttpRequest(method, path, headers, socket.getOutputStream());
|
||||
}
|
||||
catch (IOException | NullPointerException e) { return null; }
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
package me.topchetoeu.j2s.lib.debug;
|
||||
package me.topchetoeu.j2s.repl.debug;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
@ -1,4 +1,4 @@
|
||||
package me.topchetoeu.j2s.lib.debug;
|
||||
package me.topchetoeu.j2s.repl.debug;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.WeakHashMap;
|
||||
@ -69,8 +69,14 @@ public class SimpleDebugHandler implements DebugHandler {
|
||||
if (debugger != null) debugger.onFunctionLoad(func, map);
|
||||
}
|
||||
|
||||
private SimpleDebugHandler(boolean enabled) {
|
||||
if (enabled) {
|
||||
sources = new HashMap<>();
|
||||
maps = new WeakHashMap<>();
|
||||
}
|
||||
}
|
||||
|
||||
public SimpleDebugHandler() {
|
||||
sources = new HashMap<>();
|
||||
maps = new WeakHashMap<>();
|
||||
this(true);
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package me.topchetoeu.j2s.lib.debug;
|
||||
package me.topchetoeu.j2s.repl.debug;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
@ -1,4 +1,4 @@
|
||||
package me.topchetoeu.j2s.lib.debug;
|
||||
package me.topchetoeu.j2s.repl.debug;
|
||||
|
||||
import me.topchetoeu.j2s.common.Environment;
|
||||
import me.topchetoeu.j2s.runtime.Frame;
|
19
repl/src/main/java/me/topchetoeu/j2s/repl/debug/V8Error.java
Normal file
19
repl/src/main/java/me/topchetoeu/j2s/repl/debug/V8Error.java
Normal file
@ -0,0 +1,19 @@
|
||||
package me.topchetoeu.j2s.repl.debug;
|
||||
|
||||
import me.topchetoeu.j2s.compilation.json.JSON;
|
||||
import me.topchetoeu.j2s.compilation.json.JSONMap;
|
||||
|
||||
public class V8Error {
|
||||
public final String message;
|
||||
|
||||
public V8Error(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return JSON.stringify(new JSONMap().set("error", new JSONMap()
|
||||
.set("message", message)
|
||||
));
|
||||
}
|
||||
}
|
22
repl/src/main/java/me/topchetoeu/j2s/repl/debug/V8Event.java
Normal file
22
repl/src/main/java/me/topchetoeu/j2s/repl/debug/V8Event.java
Normal file
@ -0,0 +1,22 @@
|
||||
package me.topchetoeu.j2s.repl.debug;
|
||||
|
||||
import me.topchetoeu.j2s.compilation.json.JSON;
|
||||
import me.topchetoeu.j2s.compilation.json.JSONMap;
|
||||
|
||||
public class V8Event {
|
||||
public final String name;
|
||||
public final JSONMap params;
|
||||
|
||||
public V8Event(String name, JSONMap params) {
|
||||
this.name = name;
|
||||
this.params = params;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return JSON.stringify(new JSONMap()
|
||||
.set("method", name)
|
||||
.set("params", params)
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
package me.topchetoeu.j2s.repl.debug;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import me.topchetoeu.j2s.compilation.json.JSON;
|
||||
import me.topchetoeu.j2s.compilation.json.JSONElement;
|
||||
import me.topchetoeu.j2s.compilation.json.JSONMap;
|
||||
|
||||
public class V8Message {
|
||||
public final String name;
|
||||
public final int id;
|
||||
public final JSONMap params;
|
||||
|
||||
public V8Message(String name, int id, Map<String, JSONElement> params) {
|
||||
this.name = name;
|
||||
this.params = new JSONMap(params);
|
||||
this.id = id;
|
||||
}
|
||||
public V8Result respond(JSONMap result) {
|
||||
return new V8Result(id, result);
|
||||
}
|
||||
public V8Result respond() {
|
||||
return new V8Result(id, new JSONMap());
|
||||
}
|
||||
|
||||
public V8Message(JSONMap raw) {
|
||||
if (!raw.isNumber("id")) throw new IllegalArgumentException("Expected number property 'id'.");
|
||||
if (!raw.isString("method")) throw new IllegalArgumentException("Expected string property 'method'.");
|
||||
|
||||
this.name = raw.string("method");
|
||||
this.id = (int)raw.number("id");
|
||||
this.params = raw.contains("params") ? raw.map("params") : new JSONMap();
|
||||
}
|
||||
public V8Message(String raw) {
|
||||
this(JSON.parse(null, raw).map());
|
||||
}
|
||||
|
||||
public JSONMap toMap() {
|
||||
var res = new JSONMap();
|
||||
return res;
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return JSON.stringify(new JSONMap()
|
||||
.set("method", name)
|
||||
.set("params", params)
|
||||
.set("id", id)
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package me.topchetoeu.j2s.repl.debug;
|
||||
|
||||
import me.topchetoeu.j2s.compilation.json.JSON;
|
||||
import me.topchetoeu.j2s.compilation.json.JSONMap;
|
||||
|
||||
public class V8Result {
|
||||
public final int id;
|
||||
public final JSONMap result;
|
||||
|
||||
public V8Result(int id, JSONMap result) {
|
||||
this.id = id;
|
||||
this.result = result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return JSON.stringify(new JSONMap()
|
||||
.set("id", id)
|
||||
.set("result", result)
|
||||
);
|
||||
}
|
||||
}
|
186
repl/src/main/java/me/topchetoeu/j2s/repl/debug/WebSocket.java
Normal file
186
repl/src/main/java/me/topchetoeu/j2s/repl/debug/WebSocket.java
Normal file
@ -0,0 +1,186 @@
|
||||
package me.topchetoeu.j2s.repl.debug;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.Socket;
|
||||
|
||||
import me.topchetoeu.j2s.repl.debug.WebSocketMessage.Type;
|
||||
|
||||
public class WebSocket implements AutoCloseable {
|
||||
public long maxLength = 1 << 20;
|
||||
|
||||
private Socket socket;
|
||||
private boolean closed = false;
|
||||
|
||||
private OutputStream out() throws IOException {
|
||||
return socket.getOutputStream();
|
||||
}
|
||||
private InputStream in() throws IOException {
|
||||
return socket.getInputStream();
|
||||
}
|
||||
|
||||
private long readLen(int byteLen) throws IOException {
|
||||
long res = 0;
|
||||
|
||||
if (byteLen == 126) {
|
||||
res |= in().read() << 8;
|
||||
res |= in().read();
|
||||
return res;
|
||||
}
|
||||
else if (byteLen == 127) {
|
||||
res |= in().read() << 56;
|
||||
res |= in().read() << 48;
|
||||
res |= in().read() << 40;
|
||||
res |= in().read() << 32;
|
||||
res |= in().read() << 24;
|
||||
res |= in().read() << 16;
|
||||
res |= in().read() << 8;
|
||||
res |= in().read();
|
||||
return res;
|
||||
}
|
||||
else return byteLen;
|
||||
}
|
||||
private byte[] readMask(boolean has) throws IOException {
|
||||
if (has) {
|
||||
return new byte[] {
|
||||
(byte)in().read(),
|
||||
(byte)in().read(),
|
||||
(byte)in().read(),
|
||||
(byte)in().read()
|
||||
};
|
||||
}
|
||||
else return new byte[4];
|
||||
}
|
||||
|
||||
private void writeLength(int len) throws IOException {
|
||||
if (len < 126) {
|
||||
out().write((int)len);
|
||||
}
|
||||
else if (len <= 0xFFFF) {
|
||||
out().write(126);
|
||||
out().write((int)(len >> 8) & 0xFF);
|
||||
out().write((int)len & 0xFF);
|
||||
}
|
||||
else {
|
||||
out().write(127);
|
||||
out().write(0);
|
||||
out().write(0);
|
||||
out().write(0);
|
||||
out().write(0);
|
||||
out().write((len >> 24) & 0xFF);
|
||||
out().write((len >> 16) & 0xFF);
|
||||
out().write((len >> 8) & 0xFF);
|
||||
out().write(len & 0xFF);
|
||||
}
|
||||
}
|
||||
private synchronized void write(int type, byte[] data) throws IOException {
|
||||
out().write(type | 0x80);
|
||||
writeLength(data.length);
|
||||
out().write(data);
|
||||
}
|
||||
|
||||
public void send(String data) throws IOException {
|
||||
if (closed) throw new IllegalStateException("Websocket is closed.");
|
||||
write(1, data.getBytes());
|
||||
}
|
||||
public void send(byte[] data) throws IOException {
|
||||
if (closed) throw new IllegalStateException("Websocket is closed.");
|
||||
write(2, data);
|
||||
}
|
||||
public void send(WebSocketMessage msg) throws IOException {
|
||||
if (msg.type == Type.Binary) send(msg.binaryData());
|
||||
else send(msg.textData());
|
||||
}
|
||||
public void send(Object data) throws IOException {
|
||||
if (closed) throw new IllegalStateException("Websocket is closed.");
|
||||
write(1, data.toString().getBytes());
|
||||
}
|
||||
|
||||
public void close(String reason) {
|
||||
if (socket != null) {
|
||||
try {
|
||||
write(8, reason.getBytes());
|
||||
socket.close();
|
||||
}
|
||||
catch (Throwable e) { }
|
||||
}
|
||||
|
||||
socket = null;
|
||||
closed = true;
|
||||
}
|
||||
public void close() {
|
||||
close("");
|
||||
}
|
||||
|
||||
private WebSocketMessage fail(String reason) {
|
||||
System.out.println("WebSocket Error: " + reason);
|
||||
close(reason);
|
||||
return null;
|
||||
}
|
||||
|
||||
private byte[] readData() throws IOException {
|
||||
var maskLen = in().read();
|
||||
var hasMask = (maskLen & 0x80) != 0;
|
||||
var len = (int)readLen(maskLen & 0x7F);
|
||||
var mask = readMask(hasMask);
|
||||
|
||||
if (len > maxLength) fail("WebSocket Error: client exceeded configured max message size");
|
||||
else {
|
||||
var buff = new byte[len];
|
||||
|
||||
if (in().read(buff) < len) fail("WebSocket Error: payload too short");
|
||||
else {
|
||||
for (int i = 0; i < len; i++) {
|
||||
buff[i] ^= mask[(int)(i % 4)];
|
||||
}
|
||||
return buff;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public WebSocketMessage receive() throws IOException {
|
||||
var data = new ByteArrayOutputStream();
|
||||
var type = 0;
|
||||
|
||||
while (socket != null && !closed) {
|
||||
var finId = in().read();
|
||||
if (finId < 0) break;
|
||||
var fin = (finId & 0x80) != 0;
|
||||
int id = finId & 0x0F;
|
||||
|
||||
if (id == 0x8) { close(); return null; }
|
||||
if (id >= 0x8) {
|
||||
if (!fin) return fail("WebSocket Error: client-sent control frame was fragmented");
|
||||
if (id == 0x9) write(0xA, data.toByteArray());
|
||||
continue;
|
||||
}
|
||||
|
||||
if (type == 0) type = id;
|
||||
if (type == 0) return fail("WebSocket Error: client used opcode 0x00 for first fragment");
|
||||
|
||||
var buff = readData();
|
||||
if (buff == null) break;
|
||||
|
||||
if (data.size() + buff.length > maxLength) return fail("WebSocket Error: client exceeded configured max message size");
|
||||
data.write(buff);
|
||||
|
||||
if (!fin) continue;
|
||||
var raw = data.toByteArray();
|
||||
|
||||
if (type == 1) {
|
||||
return new WebSocketMessage(new String(raw));
|
||||
}
|
||||
else return new WebSocketMessage(raw);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public WebSocket(Socket socket) {
|
||||
this.socket = socket;
|
||||
}
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
package me.topchetoeu.j2s.repl.debug;
|
||||
|
||||
public class WebSocketMessage {
|
||||
public static enum Type {
|
||||
Text,
|
||||
Binary,
|
||||
}
|
||||
|
||||
public final Type type;
|
||||
private final Object data;
|
||||
|
||||
public final String textData() {
|
||||
if (type != Type.Text) throw new IllegalStateException("Message is not text.");
|
||||
return (String)data;
|
||||
}
|
||||
public final byte[] binaryData() {
|
||||
if (type != Type.Binary) throw new IllegalStateException("Message is not binary.");
|
||||
return (byte[])data;
|
||||
}
|
||||
|
||||
public WebSocketMessage(String data) {
|
||||
this.type = Type.Text;
|
||||
this.data = data;
|
||||
}
|
||||
public WebSocketMessage(byte[] data) {
|
||||
this.type = Type.Binary;
|
||||
this.data = data;
|
||||
}
|
||||
}
|
@ -1,30 +1,30 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>J2S Debugger</title>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>J2S Debugger</title>
|
||||
</head>
|
||||
<body>
|
||||
<p>
|
||||
This is the debugger of J2S. It implement the <a href="https://chromedevtools.github.io/devtools-protocol/1-2/">V8 Debugging protocol</a>,
|
||||
so you can use the devtools in chrome.<br>
|
||||
The debugger is still in early development, so please report any issues to
|
||||
<a href="https://git.topcheto.eu/topchetoeu/j2s/issues">the git repo</a>.
|
||||
</p>
|
||||
<p>
|
||||
This is the debugger of J2S. It implement the <a href="https://chromedevtools.github.io/devtools-protocol/1-2/">V8 Debugging protocol</a>,
|
||||
so you can use the devtools in chrome.<br>
|
||||
The debugger is still in early development, so please report any issues to
|
||||
<a href="https://git.topcheto.eu/topchetoeu/j2s/issues">the git repo</a>.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
Here are the available entrypoints:
|
||||
<ul>
|
||||
<li><a href="json/version">/json/version</a> - version and other stuff about the J2S engine</li>
|
||||
<li><a href="json/list">/json/list</a> - a list of all entrypoints</li>
|
||||
<li><a href="json/protocol">/json/protocol</a> - documentation of the implemented V8 protocol</li>
|
||||
<li>/(any target) - websocket entrypoints for debugging</li>
|
||||
</ul>
|
||||
</p>
|
||||
<p>
|
||||
Here are the available entrypoints:
|
||||
<ul>
|
||||
<li><a href="json/version">/json/version</a> - version and other stuff about the J2S engine</li>
|
||||
<li><a href="json/list">/json/list</a> - a list of all entrypoints</li>
|
||||
<li><a href="json/protocol">/json/protocol</a> - documentation of the implemented V8 protocol</li>
|
||||
<li>/(any target) - websocket entrypoints for debugging</li>
|
||||
</ul>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
Running ${NAME} v${VERSION} by ${AUTHOR}
|
||||
</p>
|
||||
<p>
|
||||
Running ${NAME} v${VERSION} by ${AUTHOR}
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user