Skip to content

Commit

Permalink
Merge pull request #19 from Chia-Mine/v0.0.18
Browse files Browse the repository at this point in the history
V0.0.18
  • Loading branch information
ChiaMineJP authored Jul 11, 2021
2 parents 3c7d6d9 + bc109e9 commit 8bac48a
Show file tree
Hide file tree
Showing 12 changed files with 145 additions and 19 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## [0.0.18]
### Changed
- Fixed an issue where `int_to_bytes` did not work as expected if the argument is a negative number.
- Changed `Bytes::toString()` to return python's `bytes.__repr__` style string.

## [0.0.17]
### Changed
- Updated `jscrypto` version to 1.0.2
Expand Down Expand Up @@ -108,6 +113,7 @@
Initial (beta) release.

<!--[Unreleased]: https://github.com/Chia-Mine/clvm-js/compare/v0.0.1...v0.0.2-->
[0.0.18]: https://github.com/Chia-Mine/clvm-js/compare/v0.0.17...v0.0.18
[0.0.17]: https://github.com/Chia-Mine/clvm-js/compare/v0.0.16...v0.0.17
[0.0.16]: https://github.com/Chia-Mine/clvm-js/compare/v0.0.15...v0.0.16
[0.0.15]: https://github.com/Chia-Mine/clvm-js/compare/v0.0.14...v0.0.15
Expand Down
4 changes: 2 additions & 2 deletions dist/SExp.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,10 @@ class SExp extends CLVMObject_1.CLVMObject {
return as_javascript_1.as_javascript(this);
}
toString() {
return this.as_bin().toString();
return this.as_bin().hex();
}
__repr__() {
return `SExp(${this.as_bin().toString()})`;
return `SExp(${this.as_bin().hex()})`;
}
}
exports.SExp = SExp;
Expand Down
6 changes: 6 additions & 0 deletions dist/__type_compatibility__.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ import { Word32Array } from "jscrypto/Word32Array";
import { None, str } from "./__python_types__";
import { G1Element } from "@chiamine/bls-signatures";
export declare function to_hexstr(r: Uint8Array): string;
/**
* Get python's bytes.__repr__ style string.
* @see https://github.com/python/cpython/blob/main/Objects/bytesobject.c#L1337
* @param {Uint8Array} r - byteArray to stringify
*/
export declare function PyBytes_Repr(r: Uint8Array): string;
export declare type BytesFromType = "hex" | "utf8" | "G1Element";
/**
* Unlike python, there is no immutable byte type in javascript.
Expand Down
62 changes: 57 additions & 5 deletions dist/__type_compatibility__.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Stream = exports.isIterable = exports.isList = exports.isTuple = exports.t = exports.Tuple = exports.h = exports.b = exports.Bytes = exports.to_hexstr = void 0;
exports.Stream = exports.isIterable = exports.isList = exports.isTuple = exports.t = exports.Tuple = exports.h = exports.b = exports.Bytes = exports.PyBytes_Repr = exports.to_hexstr = void 0;
const Hex_1 = require("jscrypto/Hex");
const Utf8_1 = require("jscrypto/Utf8");
const Word32Array_1 = require("jscrypto/Word32Array");
Expand All @@ -10,6 +10,58 @@ function to_hexstr(r) {
return (new Word32Array_1.Word32Array(r)).toString();
}
exports.to_hexstr = to_hexstr;
/**
* Get python's bytes.__repr__ style string.
* @see https://github.com/python/cpython/blob/main/Objects/bytesobject.c#L1337
* @param {Uint8Array} r - byteArray to stringify
*/
function PyBytes_Repr(r) {
let squotes = 0;
let dquotes = 0;
for (let i = 0; i < r.length; i++) {
const b = r[i];
const c = String.fromCodePoint(b);
switch (c) {
case "'":
squotes++;
break;
case "\"":
dquotes++;
break;
}
}
let quote = "'";
if (squotes && !dquotes) {
quote = "\"";
}
let s = "b" + quote;
for (let i = 0; i < r.length; i++) {
const b = r[i];
const c = String.fromCodePoint(b);
if (c === quote || c === "\\") {
s += "\\" + c;
}
else if (c === "\t") {
s += "\\t";
}
else if (c === "\n") {
s += "\\n";
}
else if (c === "\r") {
s += "\\r";
}
else if (c < " " || b >= 0x7f) {
s += "\\x";
s += b.toString(16).padStart(2, "0");
}
else {
s += c;
}
}
s += quote;
return s;
}
exports.PyBytes_Repr = PyBytes_Repr;
/**
* Unlike python, there is no immutable byte type in javascript.
*/
Expand Down Expand Up @@ -103,19 +155,19 @@ class Bytes {
return new Bytes(this._b);
}
toString() {
return to_hexstr(this._b);
return PyBytes_Repr(this._b);
}
hex() {
return this.toString();
return to_hexstr(this._b);
}
decode() {
return Utf8_1.Utf8.stringify(this.as_word());
}
startswith(b) {
return this.toString().startsWith(b.toString());
return this.hex().startsWith(b.hex());
}
endswith(b) {
return this.toString().endsWith(b.toString());
return this.hex().endsWith(b.hex());
}
equal_to(b) {
if (!b) {
Expand Down
2 changes: 1 addition & 1 deletion dist/browser/index.js

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion dist/casts.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ function int_from_bytes(b) {
if (!b || b.length === 0) {
return 0;
}
return parseInt(b.hex(), 16);
const unsigned32 = parseInt(b.hex(), 16);
// If the first bit is 1, it is recognized as a negative number.
if (b.get_byte_at(0) & 0x80) {
return unsigned32 - (1 << (b.length * 8));
}
return unsigned32;
}
exports.int_from_bytes = int_from_bytes;
function int_to_bytes(v) {
Expand Down
2 changes: 1 addition & 1 deletion dist/operators.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ function OperatorDict(atom_op_function_map, quote_atom, apply_atom, unknown_op_h
throw new Error(`Invalid op: ${JSON.stringify(op)}`);
}
merge(dict, OperatorDict);
const f = dict[op.toString()];
const f = dict[op.hex()];
if (typeof f !== "function") {
return dict.unknown_op_handler(op, args);
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "clvm",
"version": "0.0.17",
"version": "0.0.18",
"author": "Admin ChiaMineJP <[email protected]>",
"description": "Javascript implementation of chia lisp",
"license": "MIT",
Expand Down
4 changes: 2 additions & 2 deletions src/SExp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,10 @@ export class SExp extends CLVMObject {
}

public toString(){
return this.as_bin().toString();
return this.as_bin().hex();
}

public __repr__(){
return `SExp(${this.as_bin().toString()})`;
return `SExp(${this.as_bin().hex()})`;
}
}
60 changes: 56 additions & 4 deletions src/__type_compatibility__.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,58 @@ export function to_hexstr(r: Uint8Array) {
return (new Word32Array(r)).toString();
}

/**
* Get python's bytes.__repr__ style string.
* @see https://github.com/python/cpython/blob/main/Objects/bytesobject.c#L1337
* @param {Uint8Array} r - byteArray to stringify
*/
export function PyBytes_Repr(r: Uint8Array) {
let squotes = 0;
let dquotes = 0;
for(let i=0;i<r.length;i++){
const b = r[i];
const c = String.fromCodePoint(b);
switch(c){
case "'": squotes++; break;
case "\"": dquotes++; break;
}
}
let quote = "'";
if(squotes && !dquotes){
quote = "\"";
}

let s = "b" + quote;

for(let i=0;i<r.length;i++){
const b = r[i];
const c = String.fromCodePoint(b);
if(c === quote || c === "\\"){
s += "\\" + c;
}
else if(c === "\t"){
s += "\\t";
}
else if(c === "\n"){
s += "\\n";
}
else if(c === "\r"){
s += "\\r";
}
else if(c < " " || b >= 0x7f){
s += "\\x";
s += b.toString(16).padStart(2, "0");
}
else{
s += c;
}
}

s += quote;

return s;
}

export type BytesFromType = "hex"|"utf8"|"G1Element";

/**
Expand Down Expand Up @@ -120,23 +172,23 @@ export class Bytes {
}

public toString(){
return to_hexstr(this._b);
return PyBytes_Repr(this._b);
}

public hex(){
return this.toString();
return to_hexstr(this._b);
}

public decode(){
return Utf8.stringify(this.as_word());
}

public startswith(b: Bytes){
return this.toString().startsWith(b.toString());
return this.hex().startsWith(b.hex());
}

public endswith(b: Bytes){
return this.toString().endsWith(b.toString());
return this.hex().endsWith(b.hex());
}

public equal_to(b: Bytes|None){
Expand Down
7 changes: 6 additions & 1 deletion src/casts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ export function int_from_bytes(b: Bytes|None): int {
if(!b || b.length === 0){
return 0;
}
return parseInt(b.hex(), 16);
const unsigned32 = parseInt(b.hex(), 16);
// If the first bit is 1, it is recognized as a negative number.
if(b.get_byte_at(0) & 0x80){
return unsigned32 - (1 << (b.length*8));
}
return unsigned32;
}

export function int_to_bytes(v: int): Bytes {
Expand Down
2 changes: 1 addition & 1 deletion src/operators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ export function OperatorDict<A extends str = ATOMS>(

merge(dict, OperatorDict as any);

const f = (dict as Record<string, unknown>)[op.toString()];
const f = (dict as Record<string, unknown>)[op.hex()];
if(typeof f !== "function"){
return dict.unknown_op_handler(op, args);
}
Expand Down

0 comments on commit 8bac48a

Please sign in to comment.