hjs : Writing hScript in <script> HTML tag

08.09.2008 3870 1

Here you can see how it looks in the HTML document.
index.html :

<script type="text/hscript">
	var div = class( "js.Lib" ).document.getElementById( "testDiv" );
	var i = 0;
	div.innerHTML = "test" + i;
<div id="testDiv"></div>
<script type="text/hscript">
	div.innerHTML += "<br/>test" + i;
<script type="text/javascript" src="hjs.js"></script>

You just have to add a js file named hjs.js, that is a kind of bootstrap for hScript.
Once your document is loaded, hjs checks all <script> tags, looks if type is text/hscript, and parse the script.
The hjs has a bootstrap class.
BootStrap.hx :

class BootStrap
	var interp	: hscript.Interp;
	function new()
		interp = new hscript.Interp();
		interp.variables.set( "new", createInstance );
		interp.variables.set( "class", getClass );
		var s = js.Lib.document.getElementsByTagName( "script" );
		var script = "";
		for ( i in 0...s.length )
			if ( s[ i ].getAttribute( "type" ).toLowerCase() == "text/hscript" )
				script += s[ i ].innerHTML;
		parseScript( script );
	function parseScript( s : String )
		var parser = new hscript.Parser();
		var program = parser.parseString( s );
		interp.execute( program );
	function createInstance( s : String, a : Array<Dynamic> )
		return Type.createInstance( Type.resolveClass( s ), a );
	function getClass( s : String )
		return Type.resolveClass( s );
	static function main()
		var v = new BootStrap();

In order to get the hjs.js file, we have to build BootStrap like that:
build.hxml :

-js hjs.js
-main BootStrap
-lib hscript

As you can see in the example, all <script> tags are parsed from the top of the document to the bottom.
The variables are shared between different <script> blocks.
Last thing to know is that you have to add the hjs.js file at the bottom of your document in order to be able to access all part of the HTML document.
I can't see if it could be really useful but i found it funny to try ! .


08.14.2008 at 22:15 jdo

the best way to figure it out between the "technical performance" and the real interest in a production mode of this library is to benchmark this kind of sample code, don't you think ?

Write a comment