Skip to content

Commit

Permalink
Adds pseudo-constructors to .from, fixes StoneCypher/fsl#754
Browse files Browse the repository at this point in the history
  • Loading branch information
StoneCypher committed May 15, 2022
1 parent 2f7e8cb commit 94a26af
Show file tree
Hide file tree
Showing 14 changed files with 158 additions and 92 deletions.
14 changes: 7 additions & 7 deletions benchmark/results/general.chart.html
Expand Up @@ -28,7 +28,7 @@
</head>
<body>
<div class="container">
<canvas id="chart1652554793537" width="16" height="9"></canvas>
<canvas id="chart1652584461536" width="16" height="9"></canvas>
</div>
<script>
const format = (num) => {
Expand All @@ -51,18 +51,18 @@
chunked.map((chunk) => chunk.join('')).join(' ') + fractionStr
)
}
const ctx1652554793537 = document
.getElementById('chart1652554793537')
const ctx1652584461536 = document
.getElementById('chart1652584461536')
.getContext('2d')
const chart1652554793537 = new Chart(ctx1652554793537, {
const chart1652584461536 = new Chart(ctx1652584461536, {
type: 'bar',
data: {
labels: ["Blind cycle a traffic light 500 times by transition","Blind cycle a traffic light 500 times by action","Blind cycle a basic-hooked traffic light 500 times by transition","Blind cycle a named-hooked traffic light 500 times by transition","Blind cycle an any-transition traffic light 500 times by transition","Blind cycle an exit hooked traffic light 500 times by transition","Blind cycle an enter hooked traffic light 500 times by transition","Blind cycle a standard-transition hooked light by transition","Blind cycle a main-transition hooked light by transition","Blind cycle a force-transition hooked light by transition","Blind cycle a traffic light 500 times by action","Blind cycle a basic-hooked traffic light 500 times by action","Blind cycle a named-hooked traffic light 500 times by action","Blind cycle an any-action traffic light 500 times by action","Blind cycle a global-action traffic light 500 times by action","Blind cycle an exit hooked traffic light 500 times by action","Blind cycle an enter hooked traffic light 500 times by action","Blind cycle a standard transition tl 500 times by action","Blind cycle a main transition tl 500 times by action","Blind cycle a forced transition tl 500 times by action","Kitchen Sink 500 times"],
datasets: [
{
data: [6260,14725,1407,6278,6280,5487,5077,6026,5407,23709,14447,1377,1425,11431,11370,9806,9917,72562,72778,61973,9701],
backgroundColor: ["hsl(10.319999999999993, 85%, 55%)","hsl(24.276000000000003, 85%, 55%)","hsl(2.316000000000008, 85%, 55%)","hsl(10.355999999999995, 85%, 55%)","hsl(10.355999999999995, 85%, 55%)","hsl(9.048000000000007, 85%, 55%)","hsl(8.376000000000005, 85%, 55%)","hsl(9.936000000000002, 85%, 55%)","hsl(8.91600000000001, 85%, 55%)","hsl(39.096, 85%, 55%)","hsl(23.819999999999993, 85%, 55%)","hsl(2.2680000000000007, 85%, 55%)","hsl(2.3519999999999923, 85%, 55%)","hsl(18.851999999999993, 85%, 55%)","hsl(18.744000000000003, 85%, 55%)","hsl(16.163999999999998, 85%, 55%)","hsl(16.355999999999995, 85%, 55%)","hsl(119.64, 85%, 55%)","hsl(120, 85%, 55%)","hsl(102.18, 85%, 55%)","hsl(15.995999999999997, 85%, 55%)"],
borderColor: ["hsl(10.319999999999993, 85%, 55%)","hsl(24.276000000000003, 85%, 55%)","hsl(2.316000000000008, 85%, 55%)","hsl(10.355999999999995, 85%, 55%)","hsl(10.355999999999995, 85%, 55%)","hsl(9.048000000000007, 85%, 55%)","hsl(8.376000000000005, 85%, 55%)","hsl(9.936000000000002, 85%, 55%)","hsl(8.91600000000001, 85%, 55%)","hsl(39.096, 85%, 55%)","hsl(23.819999999999993, 85%, 55%)","hsl(2.2680000000000007, 85%, 55%)","hsl(2.3519999999999923, 85%, 55%)","hsl(18.851999999999993, 85%, 55%)","hsl(18.744000000000003, 85%, 55%)","hsl(16.163999999999998, 85%, 55%)","hsl(16.355999999999995, 85%, 55%)","hsl(119.64, 85%, 55%)","hsl(120, 85%, 55%)","hsl(102.18, 85%, 55%)","hsl(15.995999999999997, 85%, 55%)"],
data: [7270,14513,1456,6975,6568,6458,6104,6557,6608,25042,14323,1598,1505,11689,11799,10311,9944,62101,68655,68495,9192],
backgroundColor: ["hsl(12.708000000000004, 85%, 55%)","hsl(25.368000000000002, 85%, 55%)","hsl(2.5440000000000054, 85%, 55%)","hsl(12.191999999999997, 85%, 55%)","hsl(11.483999999999993, 85%, 55%)","hsl(11.291999999999994, 85%, 55%)","hsl(10.668000000000001, 85%, 55%)","hsl(11.459999999999997, 85%, 55%)","hsl(11.544000000000006, 85%, 55%)","hsl(43.775999999999996, 85%, 55%)","hsl(25.032, 85%, 55%)","hsl(2.795999999999998, 85%, 55%)","hsl(2.6279999999999974, 85%, 55%)","hsl(20.436, 85%, 55%)","hsl(20.627999999999997, 85%, 55%)","hsl(18.023999999999997, 85%, 55%)","hsl(17.376000000000005, 85%, 55%)","hsl(108.54, 85%, 55%)","hsl(120, 85%, 55%)","hsl(119.72399999999999, 85%, 55%)","hsl(16.068, 85%, 55%)"],
borderColor: ["hsl(12.708000000000004, 85%, 55%)","hsl(25.368000000000002, 85%, 55%)","hsl(2.5440000000000054, 85%, 55%)","hsl(12.191999999999997, 85%, 55%)","hsl(11.483999999999993, 85%, 55%)","hsl(11.291999999999994, 85%, 55%)","hsl(10.668000000000001, 85%, 55%)","hsl(11.459999999999997, 85%, 55%)","hsl(11.544000000000006, 85%, 55%)","hsl(43.775999999999996, 85%, 55%)","hsl(25.032, 85%, 55%)","hsl(2.795999999999998, 85%, 55%)","hsl(2.6279999999999974, 85%, 55%)","hsl(20.436, 85%, 55%)","hsl(20.627999999999997, 85%, 55%)","hsl(18.023999999999997, 85%, 55%)","hsl(17.376000000000005, 85%, 55%)","hsl(108.54, 85%, 55%)","hsl(120, 85%, 55%)","hsl(119.72399999999999, 85%, 55%)","hsl(16.068, 85%, 55%)"],
borderWidth: 2,
},
],
Expand Down
130 changes: 65 additions & 65 deletions benchmark/results/general.json
@@ -1,141 +1,141 @@
{
"name": "General performance suite",
"date": "2022-05-14T18:59:53.537Z",
"date": "2022-05-15T03:14:21.536Z",
"version": "1.2.0",
"results": [
{
"name": "Blind cycle a traffic light 500 times by transition",
"ops": 6260,
"margin": 6.07,
"percentSlower": 91.4
"ops": 7270,
"margin": 1.32,
"percentSlower": 89.41
},
{
"name": "Blind cycle a traffic light 500 times by action",
"ops": 14725,
"margin": 1.01,
"percentSlower": 79.77
"ops": 14513,
"margin": 0.53,
"percentSlower": 78.86
},
{
"name": "Blind cycle a basic-hooked traffic light 500 times by transition",
"ops": 1407,
"margin": 1.15,
"percentSlower": 98.07
"ops": 1456,
"margin": 0.89,
"percentSlower": 97.88
},
{
"name": "Blind cycle a named-hooked traffic light 500 times by transition",
"ops": 6278,
"margin": 1.42,
"percentSlower": 91.37
"ops": 6975,
"margin": 1.15,
"percentSlower": 89.84
},
{
"name": "Blind cycle an any-transition traffic light 500 times by transition",
"ops": 6280,
"margin": 1.13,
"percentSlower": 91.37
"ops": 6568,
"margin": 1.9,
"percentSlower": 90.43
},
{
"name": "Blind cycle an exit hooked traffic light 500 times by transition",
"ops": 5487,
"margin": 3.36,
"percentSlower": 92.46
"ops": 6458,
"margin": 0.74,
"percentSlower": 90.59
},
{
"name": "Blind cycle an enter hooked traffic light 500 times by transition",
"ops": 5077,
"margin": 9.75,
"percentSlower": 93.02
"ops": 6104,
"margin": 3.63,
"percentSlower": 91.11
},
{
"name": "Blind cycle a standard-transition hooked light by transition",
"ops": 6026,
"margin": 1.33,
"percentSlower": 91.72
"ops": 6557,
"margin": 0.88,
"percentSlower": 90.45
},
{
"name": "Blind cycle a main-transition hooked light by transition",
"ops": 5407,
"margin": 7.14,
"percentSlower": 92.57
"ops": 6608,
"margin": 0.92,
"percentSlower": 90.38
},
{
"name": "Blind cycle a force-transition hooked light by transition",
"ops": 23709,
"margin": 1.1,
"percentSlower": 67.42
"ops": 25042,
"margin": 0.67,
"percentSlower": 63.52
},
{
"name": "Blind cycle a traffic light 500 times by action",
"ops": 14447,
"margin": 1.21,
"percentSlower": 80.15
"ops": 14323,
"margin": 0.77,
"percentSlower": 79.14
},
{
"name": "Blind cycle a basic-hooked traffic light 500 times by action",
"ops": 1377,
"margin": 7.34,
"percentSlower": 98.11
"ops": 1598,
"margin": 3.15,
"percentSlower": 97.67
},
{
"name": "Blind cycle a named-hooked traffic light 500 times by action",
"ops": 1425,
"margin": 2.33,
"percentSlower": 98.04
"ops": 1505,
"margin": 0.6,
"percentSlower": 97.81
},
{
"name": "Blind cycle an any-action traffic light 500 times by action",
"ops": 11431,
"margin": 1.09,
"percentSlower": 84.29
"ops": 11689,
"margin": 0.95,
"percentSlower": 82.97
},
{
"name": "Blind cycle a global-action traffic light 500 times by action",
"ops": 11370,
"margin": 1.53,
"percentSlower": 84.38
"ops": 11799,
"margin": 0.71,
"percentSlower": 82.81
},
{
"name": "Blind cycle an exit hooked traffic light 500 times by action",
"ops": 9806,
"margin": 1.73,
"percentSlower": 86.53
"ops": 10311,
"margin": 0.91,
"percentSlower": 84.98
},
{
"name": "Blind cycle an enter hooked traffic light 500 times by action",
"ops": 9917,
"margin": 3.24,
"percentSlower": 86.37
"ops": 9944,
"margin": 1.25,
"percentSlower": 85.52
},
{
"name": "Blind cycle a standard transition tl 500 times by action",
"ops": 72562,
"margin": 1.08,
"percentSlower": 0.3
"ops": 62101,
"margin": 2.65,
"percentSlower": 9.55
},
{
"name": "Blind cycle a main transition tl 500 times by action",
"ops": 72778,
"margin": 1.15,
"ops": 68655,
"margin": 0.84,
"percentSlower": 0
},
{
"name": "Blind cycle a forced transition tl 500 times by action",
"ops": 61973,
"margin": 8.43,
"percentSlower": 14.85
"ops": 68495,
"margin": 0.62,
"percentSlower": 0.23
},
{
"name": "Kitchen Sink 500 times",
"ops": 9701,
"margin": 0.64,
"percentSlower": 86.67
"ops": 9192,
"margin": 1.33,
"percentSlower": 86.61
}
],
"fastest": {
"name": "Blind cycle a main transition tl 500 times by action",
"index": 18
},
"slowest": {
"name": "Blind cycle a basic-hooked traffic light 500 times by action",
"index": 11
"name": "Blind cycle a basic-hooked traffic light 500 times by transition",
"index": 2
}
}
2 changes: 1 addition & 1 deletion dist/es6/jssm.d.ts
Expand Up @@ -129,5 +129,5 @@ declare class Machine<mDT> {
sm(template_strings: TemplateStringsArray, ...remainder: any[]): Machine<mDT>;
}
declare function sm<mDT>(template_strings: TemplateStringsArray, ...remainder: any[]): Machine<mDT>;
declare function from<mDT>(MachineAsString: string): Machine<mDT>;
declare function from<mDT>(MachineAsString: string, ExtraConstructorFields?: Partial<JssmGenericConfig<mDT>> | undefined): Machine<mDT>;
export { version, transfer_state_properties, Machine, make, wrap_parse as parse, compile, sm, from, arrow_direction, arrow_left_kind, arrow_right_kind, seq, weighted_rand_select, histograph, weighted_sample_select, weighted_histo_key };
10 changes: 7 additions & 3 deletions dist/es6/jssm.js
Expand Up @@ -322,7 +322,7 @@ function transfer_state_properties(state_decl) {
class Machine {
// whargarbl this badly needs to be broken up, monolith master
constructor({ start_states, complete = [], transitions, machine_author, machine_comment, machine_contributor, machine_definition, machine_language, machine_license, machine_name, machine_version, state_declaration, fsl_version, dot_preamble = undefined, arrange_declaration = [], arrange_start_declaration = [], arrange_end_declaration = [], theme = 'default', flow = 'down', graph_layout = 'dot', instance_name }) {
this._instance_name = undefined;
this._instance_name = instance_name;
this._state = start_states[0];
this._states = new Map();
this._state_declarations = new Map();
Expand Down Expand Up @@ -1033,8 +1033,12 @@ function sm(template_strings, ...remainder /* , arguments */) {
/* eslint-enable prefer-rest-params */
)));
}
function from(MachineAsString) {
return new Machine(make(MachineAsString));
function from(MachineAsString, ExtraConstructorFields) {
const to_decorate = make(MachineAsString);
if (ExtraConstructorFields !== undefined) {
Object.keys(ExtraConstructorFields).map(key => to_decorate[key] = ExtraConstructorFields[key]);
}
return new Machine(to_decorate);
}
export { version, transfer_state_properties, Machine, make, wrap_parse as parse, compile, sm, from, arrow_direction, arrow_left_kind, arrow_right_kind,
// WHARGARBL TODO these should be exported to a utility library
Expand Down
2 changes: 1 addition & 1 deletion dist/jssm.es5.cjs.js

Large diffs are not rendered by default.

10 changes: 7 additions & 3 deletions dist/jssm.es5.cjs.nonmin.js
Expand Up @@ -16208,7 +16208,7 @@ function transfer_state_properties(state_decl) {
class Machine {
// whargarbl this badly needs to be broken up, monolith master
constructor({ start_states, complete = [], transitions, machine_author, machine_comment, machine_contributor, machine_definition, machine_language, machine_license, machine_name, machine_version, state_declaration, fsl_version, dot_preamble = undefined, arrange_declaration = [], arrange_start_declaration = [], arrange_end_declaration = [], theme = 'default', flow = 'down', graph_layout = 'dot', instance_name }) {
this._instance_name = undefined;
this._instance_name = instance_name;
this._state = start_states[0];
this._states = new Map();
this._state_declarations = new Map();
Expand Down Expand Up @@ -16919,8 +16919,12 @@ function sm(template_strings, ...remainder /* , arguments */) {
/* eslint-enable prefer-rest-params */
)));
}
function from(MachineAsString) {
return new Machine(make(MachineAsString));
function from(MachineAsString, ExtraConstructorFields) {
const to_decorate = make(MachineAsString);
if (ExtraConstructorFields !== undefined) {
Object.keys(ExtraConstructorFields).map(key => to_decorate[key] = ExtraConstructorFields[key]);
}
return new Machine(to_decorate);
}

exports.Machine = Machine;
Expand Down
2 changes: 1 addition & 1 deletion dist/jssm.es5.iife.js

Large diffs are not rendered by default.

10 changes: 7 additions & 3 deletions dist/jssm.es5.iife.nonmin.js
Expand Up @@ -16207,7 +16207,7 @@ var jssm = (function (exports) {
class Machine {
// whargarbl this badly needs to be broken up, monolith master
constructor({ start_states, complete = [], transitions, machine_author, machine_comment, machine_contributor, machine_definition, machine_language, machine_license, machine_name, machine_version, state_declaration, fsl_version, dot_preamble = undefined, arrange_declaration = [], arrange_start_declaration = [], arrange_end_declaration = [], theme = 'default', flow = 'down', graph_layout = 'dot', instance_name }) {
this._instance_name = undefined;
this._instance_name = instance_name;
this._state = start_states[0];
this._states = new Map();
this._state_declarations = new Map();
Expand Down Expand Up @@ -16918,8 +16918,12 @@ var jssm = (function (exports) {
/* eslint-enable prefer-rest-params */
)));
}
function from(MachineAsString) {
return new Machine(make(MachineAsString));
function from(MachineAsString, ExtraConstructorFields) {
const to_decorate = make(MachineAsString);
if (ExtraConstructorFields !== undefined) {
Object.keys(ExtraConstructorFields).map(key => to_decorate[key] = ExtraConstructorFields[key]);
}
return new Machine(to_decorate);
}

exports.Machine = Machine;
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/classes/Machine.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/docs/modules.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion jssm.d.ts
Expand Up @@ -129,5 +129,5 @@ declare class Machine<mDT> {
sm(template_strings: TemplateStringsArray, ...remainder: any[]): Machine<mDT>;
}
declare function sm<mDT>(template_strings: TemplateStringsArray, ...remainder: any[]): Machine<mDT>;
declare function from<mDT>(MachineAsString: string): Machine<mDT>;
declare function from<mDT>(MachineAsString: string, ExtraConstructorFields?: Partial<JssmGenericConfig<mDT>> | undefined): Machine<mDT>;
export { version, transfer_state_properties, Machine, make, wrap_parse as parse, compile, sm, from, arrow_direction, arrow_left_kind, arrow_right_kind, seq, weighted_rand_select, histograph, weighted_sample_select, weighted_histo_key };
14 changes: 11 additions & 3 deletions src/ts/jssm.ts
Expand Up @@ -539,7 +539,7 @@ class Machine<mDT> {
instance_name
}: JssmGenericConfig<mDT>) {

this._instance_name = undefined;
this._instance_name = instance_name;

this._state = start_states[0];
this._states = new Map();
Expand Down Expand Up @@ -1489,8 +1489,16 @@ function sm<mDT>(template_strings: TemplateStringsArray, ...remainder /* , argum



function from<mDT>(MachineAsString: string): Machine<mDT> {
return new Machine( make( MachineAsString ) );
function from<mDT>(MachineAsString: string, ExtraConstructorFields?: Partial< JssmGenericConfig<mDT> > | undefined): Machine<mDT> {

const to_decorate = make( MachineAsString );

if (ExtraConstructorFields !== undefined) {
Object.keys(ExtraConstructorFields).map(key => to_decorate[key] = ExtraConstructorFields[key]);
}

return new Machine( to_decorate );

}


Expand Down
33 changes: 33 additions & 0 deletions src/ts/tests/from.spec.ts
@@ -0,0 +1,33 @@

import * as jssm from '../jssm';

const sm = jssm.sm;





describe('Creating with .from', () => {

test('doesn\'t throw', () => {

expect( () => { const _foo = jssm.from('a -> b;'); })
.not.toThrow();

});

test('matches results from sm``', () => {

expect( jssm.from('a -> b;') )
.toStrictEqual( sm`a -> b;` );

});

test('honors pseudo-constructor', () => {

expect( jssm.from('a -> b;', { instance_name: 'bob' }).instance_name() )
.toBe('bob');

});

});

0 comments on commit 94a26af

Please sign in to comment.