Skip to content

Commit

Permalink
"isXXX" builtins (#133)
Browse files Browse the repository at this point in the history
Builtins for testing whether a value is of a certain type, should be relatively simple
  • Loading branch information
arduano authored May 30, 2024
1 parent f0fd126 commit 6f8c917
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 37 deletions.
22 changes: 12 additions & 10 deletions nixjs-rt/src/builtins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@ import { typeMismatchError } from "./errors/typeError";
import {
Attrset,
EvalCtx,
EvalException,
FALSE,
Lambda,
NULL,
NixBool,
NixFloat,
NixInt,
NixList,
NixNull,
NixString,
NixType,
NixTypeClass,
Path,
TRUE,
nixBoolFromJs,
} from "./lib";
import { dirOf, isAbsolutePath, normalizePath } from "./utils";

Expand Down Expand Up @@ -385,39 +387,39 @@ export function getBuiltins() {
},

isAttrs: (arg) => {
throw new Error("unimplemented");
return nixBoolFromJs(arg instanceof Attrset);
},

isBool: (arg) => {
throw new Error("unimplemented");
return nixBoolFromJs(arg instanceof NixBool);
},

isFloat: (arg) => {
throw new Error("unimplemented");
return nixBoolFromJs(arg instanceof NixFloat);
},

isFunction: (arg) => {
throw new Error("unimplemented");
return nixBoolFromJs(arg instanceof Lambda);
},

isInt: (arg) => {
throw new Error("unimplemented");
return nixBoolFromJs(arg instanceof NixInt);
},

isList: (arg) => {
throw new Error("unimplemented");
return nixBoolFromJs(arg instanceof NixList);
},

isNull: (arg) => {
throw new Error("unimplemented");
return nixBoolFromJs(arg instanceof NixNull);
},

isPath: (arg) => {
throw new Error("unimplemented");
return nixBoolFromJs(arg instanceof Path);
},

isString: (arg) => {
throw new Error("unimplemented");
return nixBoolFromJs(arg instanceof NixString);
},

length: (arg) => {
Expand Down
55 changes: 28 additions & 27 deletions nixjs-rt/src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export abstract class NixType {
}

and(rhs: NixType): NixBool {
return _nixBoolFromJs(this.asBoolean() && rhs.asBoolean());
return nixBoolFromJs(this.asBoolean() && rhs.asBoolean());
}

apply(param: NixType): NixType {
Expand Down Expand Up @@ -183,11 +183,11 @@ export abstract class NixType {
}

implication(rhs: NixType): NixBool {
return _nixBoolFromJs(!this.asBoolean() || rhs.asBoolean());
return nixBoolFromJs(!this.asBoolean() || rhs.asBoolean());
}

invert(): NixBool {
return _nixBoolFromJs(!this.asBoolean());
return nixBoolFromJs(!this.asBoolean());
}

/**
Expand Down Expand Up @@ -228,7 +228,7 @@ export abstract class NixType {
}

or(rhs: NixType): NixBool {
return _nixBoolFromJs(this.asBoolean() || rhs.asBoolean());
return nixBoolFromJs(this.asBoolean() || rhs.asBoolean());
}

select(attrPath: NixType[], defaultValue: NixType | undefined): NixType {
Expand Down Expand Up @@ -333,7 +333,7 @@ export class NixBool extends NixType {
if (!(rhs instanceof NixBool)) {
return FALSE;
}
return _nixBoolFromJs(this.value === rhs.value);
return nixBoolFromJs(this.value === rhs.value);
}
}

Expand Down Expand Up @@ -392,7 +392,7 @@ export abstract class Attrset extends NixType implements Scope {
}
foundValue = foundValue.get(attrName);
}
return _nixBoolFromJs(foundValue !== undefined);
return nixBoolFromJs(foundValue !== undefined);
}

/**
Expand Down Expand Up @@ -669,21 +669,21 @@ export class NixFloat extends NixType {
override eq(rhs: NixType): NixBool {
rhs = rhs.toStrict();
if (rhs instanceof NixInt) {
return _nixBoolFromJs(this.value === rhs.number);
return nixBoolFromJs(this.value === rhs.number);
}
if (rhs instanceof NixFloat) {
return _nixBoolFromJs(this.value === rhs.value);
return nixBoolFromJs(this.value === rhs.value);
}
return FALSE;
}

override less(rhs: NixType): NixBool {
rhs = rhs.toStrict();
if (rhs instanceof NixInt) {
return _nixBoolFromJs(this.value < rhs.number);
return nixBoolFromJs(this.value < rhs.number);
}
if (rhs instanceof NixFloat) {
return _nixBoolFromJs(this.value < rhs.value);
return nixBoolFromJs(this.value < rhs.value);
}
return super.less(rhs);
}
Expand Down Expand Up @@ -773,21 +773,21 @@ export class NixInt extends NixType {
override eq(rhs: NixType): NixBool {
rhs = rhs.toStrict();
if (rhs instanceof NixInt) {
return _nixBoolFromJs(this.int64 === rhs.int64);
return nixBoolFromJs(this.int64 === rhs.int64);
}
if (rhs instanceof NixFloat) {
return _nixBoolFromJs(this.number === rhs.value);
return nixBoolFromJs(this.number === rhs.value);
}
return super.eq(rhs);
}

override less(rhs: NixType): NixBool {
rhs = rhs.toStrict();
if (rhs instanceof NixInt) {
return _nixBoolFromJs(this.int64 < rhs.int64);
return nixBoolFromJs(this.int64 < rhs.int64);
}
if (rhs instanceof NixFloat) {
return _nixBoolFromJs(this.number < rhs.value);
return nixBoolFromJs(this.number < rhs.value);
}
return super.less(rhs);
}
Expand Down Expand Up @@ -894,7 +894,7 @@ export class NixList extends NixType {
return TRUE;
}
}
return _nixBoolFromJs(this.values.length < rhs.values.length);
return nixBoolFromJs(this.values.length < rhs.values.length);
}

toJs(): NixType[] {
Expand All @@ -916,7 +916,7 @@ export class NixList extends NixType {

export class NixNull extends NixType {
override eq(rhs: NixType): NixBool {
return _nixBoolFromJs(rhs.toStrict() instanceof NixNull);
return nixBoolFromJs(rhs.toStrict() instanceof NixNull);
}

toJs(): boolean {
Expand All @@ -936,10 +936,6 @@ export class NixNull extends NixType {
}
}

export const NULL = new NixNull();
export const TRUE = new NixBool(true);
export const FALSE = new NixBool(false);

export class NixString extends NixType {
readonly value: string;

Expand Down Expand Up @@ -968,15 +964,15 @@ export class NixString extends NixType {
if (!(rhs instanceof NixString)) {
return FALSE;
}
return _nixBoolFromJs(this.value === rhs.value);
return nixBoolFromJs(this.value === rhs.value);
}

override less(rhs: NixType): NixBool {
rhs = rhs.toStrict();
if (!(rhs instanceof NixString)) {
return super.less(rhs);
}
return _nixBoolFromJs(this.value < rhs.value);
return nixBoolFromJs(this.value < rhs.value);
}

toJs(): string {
Expand Down Expand Up @@ -1020,7 +1016,7 @@ export class Path extends NixType {
if (!(rhs instanceof Path)) {
return super.less(rhs);
}
return _nixBoolFromJs(this.path < rhs.path);
return nixBoolFromJs(this.path < rhs.path);
}

asString(): string {
Expand Down Expand Up @@ -1204,6 +1200,15 @@ export class Lambda extends NixType {
}
}

export const NULL = new NixNull();
export const TRUE = new NixBool(true);
export const FALSE = new NixBool(false);

// For creating a bool without allocating a new object.
export function nixBoolFromJs(value: boolean): NixBool {
return value ? TRUE : FALSE;
}

// Attrset:
export function attrset(evalCtx: EvalCtx, entries: AttrsetBody): Attrset {
return new LazyAttrset(evalCtx, false, entries);
Expand Down Expand Up @@ -1334,10 +1339,6 @@ function _attrPathToValue(
});
}

function _nixBoolFromJs(value: boolean): NixBool {
return value ? TRUE : FALSE;
}

function _buildGlobalScope() {
const scope = new Map();
const builtins = _createBuiltinsAttrset();
Expand Down
2 changes: 2 additions & 0 deletions nixjs-rt/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { NixBool, NixNull } from "./lib";

export function isAbsolutePath(path: string): boolean {
return path.startsWith("/");
}
Expand Down
Loading

0 comments on commit 6f8c917

Please sign in to comment.