PORTS: Support all serializable data. (#1089)

A significant portion of players who use ports are passing objects through them. Currently they are required to handle that themselves via JSON serialization. This PR adds better support for passing objects; which is more convenient, extensive, and optimized (probably, more on this one later).

This adds zero overhead to existing (or when passing any primitive types) port usage, and also isn't a breaking change. The questions to debate here are:

Should objects be supported in the first place?
If so, how exactly do we want to serialize objects?
Based on an extensive discussion in Discord, the overwhelming majority answered "yes" to question one. As for question two, that has been much more hotly contested.

Ultimately, `structuredClone` was used, despite less-than-stellar performance, because other options were worse either in safety, speed, error-handling, or multiple of the above.
This commit is contained in:
LJ
2024-02-17 20:15:17 -07:00
committed by GitHub
parent aba2336093
commit 27a8abbdec
3 changed files with 33 additions and 50 deletions
-12
View File
@@ -1326,12 +1326,6 @@ export const ns: InternalAPI<NSFull> = {
},
writePort: (ctx) => (_portNumber, data) => {
const portNumber = helpers.portNumber(ctx, _portNumber);
if (typeof data !== "string" && typeof data !== "number") {
throw helpers.makeRuntimeErrorMsg(
ctx,
`Trying to write invalid data to a port: only strings and numbers are valid.`,
);
}
return writePort(portNumber, data);
},
write: (ctx) => (_filename, _data, _mode) => {
@@ -1364,12 +1358,6 @@ export const ns: InternalAPI<NSFull> = {
},
tryWritePort: (ctx) => (_portNumber, data) => {
const portNumber = helpers.portNumber(ctx, _portNumber);
if (typeof data !== "string" && typeof data !== "number") {
throw helpers.makeRuntimeErrorMsg(
ctx,
`Trying to write invalid data to a port: only strings and numbers are valid.`,
);
}
return tryWritePort(portNumber, data);
},
nextPortWrite: (ctx) => (_portNumber) => {