Skip to content

Commit

Permalink
fix(compiler): allow empty programs to be compiled
Browse files Browse the repository at this point in the history
  • Loading branch information
bitjson committed Aug 21, 2019
1 parent 68f7a1c commit 41275c0
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 29 deletions.
159 changes: 150 additions & 9 deletions src/lib/auth/templates/language/compile.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import test from 'ava';

import { hexToBin } from '../../../utils/hex';

import { compileScript } from './compile';
import { compileScript, compileScriptText } from './compile';

test('compileScript: unprovided ID', t => {
t.deepEqual(compileScript('test', {}, { scripts: { typo: '1' } }), {
Expand All @@ -26,19 +26,160 @@ test('compileScript: unprovided ID', t => {

test('compileScript: empty string', t => {
t.deepEqual(compileScript('t', {}, { scripts: { t: '' } }), {
errorType: 'parse',
errors: [
bytecode: Uint8Array.of(),
parse: {
end: {
column: 1,
line: 1,
offset: 0
},
name: 'Script',
start: {
column: 1,
line: 1,
offset: 0
},
value: []
},
reduce: {
bytecode: Uint8Array.of(),
range: {
endColumn: 1,
endLineNumber: 1,
startColumn: 1,
startLineNumber: 1
},
source: [
{
bytecode: Uint8Array.of(),
range: {
endColumn: 1,
endLineNumber: 1,
startColumn: 1,
startLineNumber: 1
}
}
]
},
resolve: [
{
error: 'Tried to compile an empty string as a script.',
range: {
endColumn: 0,
endLineNumber: 0,
startColumn: 0,
startLineNumber: 0
endColumn: 1,
endLineNumber: 1,
startColumn: 1,
startLineNumber: 1
},
type: 'comment',
value: ''
}
],
success: true
});
});

test('compileScriptText: empty string', t => {
t.deepEqual(compileScriptText('', {}, { scripts: {} }), {
bytecode: Uint8Array.of(),
parse: {
end: {
column: 1,
line: 1,
offset: 0
},
name: 'Script',
start: {
column: 1,
line: 1,
offset: 0
},
value: []
},
reduce: {
bytecode: Uint8Array.of(),
range: {
endColumn: 1,
endLineNumber: 1,
startColumn: 1,
startLineNumber: 1
},
source: [
{
bytecode: Uint8Array.of(),
range: {
endColumn: 1,
endLineNumber: 1,
startColumn: 1,
startLineNumber: 1
}
}
]
},
resolve: [
{
range: {
endColumn: 1,
endLineNumber: 1,
startColumn: 1,
startLineNumber: 1
},
type: 'comment',
value: ''
}
],
success: false
success: true
});
});

test('compileScriptText: empty script (script with space)', t => {
t.deepEqual(compileScript('t', {}, { scripts: { t: ' ' } }), {
bytecode: Uint8Array.of(),
parse: {
end: {
column: 5,
line: 1,
offset: 4
},
name: 'Script',
start: {
column: 5,
line: 1,
offset: 4
},
value: []
},
reduce: {
bytecode: Uint8Array.of(),
range: {
endColumn: 5,
endLineNumber: 1,
startColumn: 5,
startLineNumber: 1
},
source: [
{
bytecode: Uint8Array.of(),
range: {
endColumn: 5,
endLineNumber: 1,
startColumn: 5,
startLineNumber: 1
}
}
]
},
resolve: [
{
range: {
endColumn: 5,
endLineNumber: 1,
startColumn: 5,
startLineNumber: 1
},
type: 'comment',
value: ''
}
],
success: true
});
});

Expand Down
18 changes: 0 additions & 18 deletions src/lib/auth/templates/language/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,24 +83,6 @@ export const compileScriptText = <
environment: CompilationEnvironment<CompilerOperationData>,
scriptId?: string
): CompilationResult<ProgramState> => {
// tslint:disable-next-line: no-if-statement
if (script.length === 0) {
return {
errorType: 'parse',
errors: [
{
error: 'Tried to compile an empty string as a script.',
range: {
endColumn: 0,
endLineNumber: 0,
startColumn: 0,
startLineNumber: 0
}
}
],
success: false
};
}
const parseResult = parseScript(script);
// tslint:disable-next-line: no-if-statement
if (!parseResult.status) {
Expand Down
9 changes: 7 additions & 2 deletions src/lib/auth/templates/language/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ enum Constants {
export const resolveScriptSegment = (
segment: BitAuthScriptSegment,
resolveIdentifiers: IdentifierResolutionFunction
): ResolvedScript =>
): ResolvedScript => {
// tslint:disable-next-line: cyclomatic-complexity
segment.value.map(child => {
const resolved = segment.value.map(child => {
const range = pluckRange(child);
switch (child.name) {
case 'Identifier':
Expand Down Expand Up @@ -159,6 +159,11 @@ export const resolveScriptSegment = (
}
});

return resolved.length === 0
? [{ range: pluckRange(segment), type: 'comment' as 'comment', value: '' }]
: resolved;
};

/**
* Returns the bytecode result on success or an error message on failure.
*/
Expand Down

0 comments on commit 41275c0

Please sign in to comment.