atom: handle truncated bytes in fromCord

This commit is contained in:
Hunter Miller 2024-07-17 00:49:19 -05:00
parent d01c0cac1e
commit fb50a411e5
5 changed files with 2280 additions and 1588 deletions

3830
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,21 +1,28 @@
{
"name": "@urbit/nockjs",
"version": "1.4.0-dev1",
"version": "1.4.0-dev2",
"description": "Javascript urbit runtime",
"main": "dist/index.js",
"module": "dist/nockjs.esm.mjs",
"types": "dist/index.d.ts",
"type": "module",
"module": "dist/nockjs.esm.js",
"exports": {
"require": "./dist/index.js",
"import": "./dist/nockjs.esm.js"
".": {
"require": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"import": {
"types": "./dist/index.d.mts",
"default": "./dist/nockjs.esm.mjs"
}
}
},
"files": [
"dist/**"
],
"scripts": {
"start": "dts watch",
"build": "dts build",
"build": "dts build && cp dist/index.d.ts dist/index.d.mts && cp dist/nockjs.esm.js dist/nockjs.esm.mjs",
"test": "dts test",
"prepare": "npm run build"
},
@ -40,7 +47,7 @@
"homepage": "https://github.com/urbit/nockjs#readme",
"devDependencies": {
"@types/jest": "^29.5.1",
"dts-cli": "^2.0.3",
"dts-cli": "^2.0.5",
"jest": "^29.5.0",
"typescript": "^5.0.4"
},

View File

@ -1,6 +1,6 @@
import { jam, cue, cue_bytes, bigintToDataView, bi_cut } from "./serial";
import bits from "./bits";
import { dwim } from "./noun-helpers";
import { dwim, enjs } from "./noun-helpers";
import { Atom, Cell, Noun } from "./noun";
import compiler from "./compiler";
import { bigIntFromStringWithRadix } from "./bigint";
@ -250,3 +250,15 @@ test("decrement", () => {
const product = context.nock(dwim(0), formula);
expect(product.equals(dwim(42))).toBeTruthy();
});
test('dwim <-> enjs.cord', () => {
const str = `=/ m (strand ,vase)
;< [=ship =desk =case] bind:m get-beak
;< ~ bind:m
(poke-our %hood %kiln-merge !>([%example ship %base case %auto]))
(pure:m !>('success'))`;
const atom = dwim(str);
const cord = enjs.cord(atom);
expect(cord).toEqual(str);
})

View File

@ -196,7 +196,8 @@ class Atom {
j,
octs = Array(str.length);
for (i = 0, j = octs.length - 1; i < octs.length; ++i, --j) {
octs[j] = (str.charCodeAt(i) & 0xff).toString(16);
const charByte = (str.charCodeAt(i) & 0xff).toString(16);
octs[j] = charByte.length === 1 ? "0" + charByte : charByte;
}
if (str.length > 4) return Atom.fromString(octs.join(''), 16);
else return new Atom(BigInt(parseInt(octs.join(""), 16)));

BIN
urbit-nockjs-1.4.0-dev2.tgz Normal file

Binary file not shown.