y-less.com | Alex “Y_Less” Cole’s blog

May/09

18

yavascript debug mode

Introduction

One of the things added to the yavascript compiler, but not completed by the time I had to hand the project in to uni, was a debug mode. Because the compiler reads all the code in syntactically it can output it in whatever form it likes, including an experimental full debug mode. Basically your code goes from:

a += /search/i;

To:

$Y._addAss(
	/* target */
	$Y._ref(
		/* name */
		'a',
		/* original code */
		'a',
		/* original line */
		42,
		/* original file */
		'my_code.js'),
	/* expression */
	$Y._regex(
		/* pattern */
		'search',
		/* modifiers */
		'i',
		/* original code */
		'/search/i',
		/* original line */
		43,
		/* original file */
		'my_code.js'),
	/* original code */
	'a +=\n\t/search/i;',
	/* original line */
	42,
	/* original file */
	'my_code.js');

The obvious question is WHY? But if you examine this code carefully you will see that it makes run-time debugging information a snap to collect and use to find problems.

Use

Given that this mode uses the compiler to generate it’s code, using this would be no more complicated than writing regular code and compiling, just compiling to a format different to minified code. It could even be written to output all the required functions in the same file, though the more sensible option would be to get the user to include a second script in the html:

<script type="text/javascript" charset=”UTF-8” src="yavascript/debug.js"></script>
<script type="text/javascript" charset=”UTF-8” src="my_code_in_debug_mode.js"></script>

To compile in debug mode would simply require the addition of an extra command line switch “-d”, so to compile the following:

var a = window['console'];

In debug mode would require:

yavascript.exe -f input_filename.ys -o output_filename.js -t web -d

_DEBUG

The introduction of debug mode adds one of the most obvious uses of the preprocessor: debug outputs. Although, given the verbose nature of the information output in debug mode, debug outputs will be less needed, there are still some things compilers can’t guess that you want to know. Debug mode in the compiler and development mode in the browser (or other development environment) will both add an internal define called “_DEBUG” allowing code such as:

a += 10;
#ifdef _DEBUG
	console.log('Did the addition at: ' + Date.now());
#endif

That code will then be included or excluded depending on the compile mode, and included in development.

Implementation

Obviously using function calls instead of standard operators requires re-writing most of javascript with additional checks for things like null objects or missing and invalid properties. One example for part of the addition function would be:

$Y =
	{
		toString :
			function (obj)
			{
				return Object.prototype.toString.call(obj);
			},
		isRegex :
			function (obj)
			{
				return this.toString(obj) === '[object RegExp]';
			},
		_add :
			function (first, second)
			{
				if (this.isRegex(first))
				{
					this.error('Can't add regular expressions:);
					this.error('first parameter: ' + first);
				}
				if (this.isRegex(second))
				{
					this.error('Can't add regular expressions:);
					this.error('second parameter: ' + second);
				}
				// Additional checks
				return first + second;
			}
	};

Functions are prefixed with ‘_’ to distinguish keywords from their debug equivalent, but this will probably be changed to something else.

Obviously far more checks than just regex can be included, such as trying to add an object to a number, or NaN to anything, but it’s just a short example.

Bugs

I’m still working on getting closures to work, but I have an idea for them. Apart from that there aren’t any bugs as the system isn’t done yet!

No tags

1 comment

  • Kylie Batt · 16 April, 2010 at 05:48

    Поздравляю, великолепная идея и своевременно…

    Introduction One of the things added to the yavascript compiler, but not completed by the time I had to hand the project in to uni, was a debug mode…..

Leave a Reply

You must be logged in to post a comment.

<<

>>

Theme Design by devolux.nh2.me