Skip to content
This repository has been archived by the owner on Nov 12, 2017. It is now read-only.

Commit

Permalink
Added @debug decorator
Browse files Browse the repository at this point in the history
This decorator's intend is to eliminate debugging functions at compile time. Currently clears the function body, only.
  • Loading branch information
dcodeIO committed May 15, 2017
1 parent 9521a9f commit 9521869
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 5 deletions.
34 changes: 34 additions & 0 deletions docs/compiled.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@
this.enclosingClass = null;
this.currentReturnType = null;
this.nextGlobalVariableOffset = 0;
this.isDebug = false;
this.boolType = null;
this.sbyteType = null;
this.errorType = null;
Expand Down Expand Up @@ -426,6 +427,10 @@
metadata(node, context, decorator);
}

else if (decorator.stringValue === "debug") {
debug(node, context, decorator);
}

else {
context.log.error(node.range, "There is no such decorator");
}
Expand Down Expand Up @@ -1824,6 +1829,7 @@
source = source.next;
}

this.context.isDebug = this.preprocessor.isDefined("NODEBUG") === false;
__imports.Profiler_end("parsing");
__imports.Profiler_begin();
var global = this.global;
Expand Down Expand Up @@ -1905,6 +1911,34 @@
return builder.append(path).append(extension).finish();
}

function debug(node, context, decorator) {
__imports.assert(node.kind === 17);

if (node.functionReturnType().stringValue !== "void") {
context.log.error(decorator.range, "This decorator cannot be used with functions specifying a return value");

return;
}

if (context.isDebug) {
return;
}

var body = node.functionBody();

if (body === null || body.childCount() === 0) {
return;
}

var child = body.firstChild;

while (child !== null) {
var next = child.nextSibling;
child.remove();
child = next;
}
}

function metadata(node, context, decorator) {
__imports.assert(node.kind === 17);
var body = node.functionBody();
Expand Down
Binary file modified docs/compiled.wasm
Binary file not shown.
2 changes: 2 additions & 0 deletions src/checker.thin
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class CheckContext {
enclosingClass: Symbol;
currentReturnType: Type;
nextGlobalVariableOffset: int;
isDebug: bool;

// Native types
boolType: Type;
Expand Down Expand Up @@ -143,6 +144,7 @@ function initialize(context: CheckContext, node: Node, parentScope: Scope, mode:
while (decorator != null) {
var name = decorator.stringValue;
if (decorator.stringValue == "metadata") metadata(node, context, decorator);
else if (decorator.stringValue == "debug") debug(node, context, decorator);
else context.log.error(node.range, "There is no such decorator");
decorator = decorator.nextSibling;
}
Expand Down
1 change: 1 addition & 0 deletions src/compiler.thin
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ class Compiler {
}
source = source.next;
}
this.context.isDebug = this.preprocessor.isDefined("NODEBUG") == false;

Profiler_end("parsing");
Profiler_begin();
Expand Down
27 changes: 27 additions & 0 deletions src/decorators/debug.thin
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Eliminates functions when compiling with the NODEBUG preprocessor flag.
// TODO: Add a compiler optimization that removes empty functions and their respective calls.

function debug(node: Node, context: CheckContext, decorator: Node): void {
assert(node.kind == NodeKind.FUNCTION);

// Recover from invalid use
if (node.functionReturnType().stringValue != "void") {
context.log.error(decorator.range, "This decorator cannot be used with functions specifying a return value");
return;
}

if (context.isDebug) return;

var body = node.functionBody();
if (body == null || body.childCount() == 0) { // Import or already empty
return;
}

// Clear the body
var child = body.firstChild;
while (child != null) {
var next = child.nextSibling;;
child.remove();
child = next;
}
}
8 changes: 3 additions & 5 deletions src/decorators/metadata.thin
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
// Injects basic metadata into the decorated function.

function metadata(node: Node, context: CheckContext, decorator: Node): void {
assert(node.kind == NodeKind.FUNCTION);

// This is an example of what decorators could do. However, without a way to extend the
// compiler with custom decorators (i.e. imports and function pointers), this feature
// won't be of much value, yet.

var body = node.functionBody();
if (body == null || body.childCount() == 0) { // No need to instrument empty functions
if (body == null || body.childCount() == 0) { // No need to instrument imports or empty functions
return;
}

Expand Down

0 comments on commit 9521869

Please sign in to comment.