www.digitalmars.com         C & C++   DMDScript  

DMDScript - DMDScript doesn't support closure at all

reply hax <hax.sfo gmail.com> writes:
I'm so disappointed that DMDScript doesn't support such core features of
ECMAScript!

If you never plan to support closure, please don't declare DMDScript as a
implementation of ECMA 262.


Test code
=========

// for browsers
if (typeof alert != 'undefined') {
	print = function () {
		alert(Array.prototype.slice.call(arguments).join(' '));
	}
}
// for jshost (jslibs)
else if (typeof LoadModule != 'undefined') {
	LoadModule('jsstd');
	print = function () {
		Print(Array.prototype.join.call(arguments, ' '), '\n');
	}
}
// for DMDScript
else if (typeof ScriptEngine == 'function' && ScriptEngine() == 'DMDScript') {
	_print = print;
	print = function () {
		_print(Array.prototype.join.call(arguments, ' '), '\n');
	}
}

void function () {

	function A(x) {
		function B() {
			return x * x;
		}
		return B;
	}

	var b1 = A(1);
	var b2 = A(2);

	print('b1() =', b1());
	print('b2() =', b2());
	print('b1', b1 == b2 ? '==' : '!=', 'b2');

}.call();

=================
Result of the browsers, rhino, jshost(SpiderMonkey):

b1() = 1
b2() = 4
b1 != b2

Result of DMDScript:

Digital Mars DMDScript 1.13
www.digitalmars.com
Compiled by Digital Mars DMD D compiler
Copyright (c) 1999-2007 by Digital Mars
written by Walter Bright
1 source files

b1() = NaN
b2() = NaN
b1 == b2


BTW, DMDScript doesn't allow definitely correct function calling syntax:

(function (s) { print(s) })('Hello world')
void function (s) { print(s) } ('Hello world')

So I must write

void function () {...}.call()

Too bad! You should fix it.
Jul 19 2007
parent Alan Knowles <alan akbkhome.com> writes:
this is fixed in my modified dmdscript

http://www.akbkhome.com/svn/gtkDS/
download a tarball here.
http://devel.akbkhome.com/gtkjs/

The problem revolves around dmdscript caching function defintions, 
rather than creating new references where they are found. It required an 
additional opcode IRfunction, and a few changes to the call method for 
ddeclaredfunction.

Regards
Alan


hax wrote:
 I'm so disappointed that DMDScript doesn't support such core features of
 ECMAScript!
 
 If you never plan to support closure, please don't declare DMDScript as a
 implementation of ECMA 262.
 
 
 Test code
 =========
 
 // for browsers
 if (typeof alert != 'undefined') {
 	print = function () {
 		alert(Array.prototype.slice.call(arguments).join(' '));
 	}
 }
 // for jshost (jslibs)
 else if (typeof LoadModule != 'undefined') {
 	LoadModule('jsstd');
 	print = function () {
 		Print(Array.prototype.join.call(arguments, ' '), '\n');
 	}
 }
 // for DMDScript
 else if (typeof ScriptEngine == 'function' && ScriptEngine() == 'DMDScript') {
 	_print = print;
 	print = function () {
 		_print(Array.prototype.join.call(arguments, ' '), '\n');
 	}
 }
 
 void function () {
 
 	function A(x) {
 		function B() {
 			return x * x;
 		}
 		return B;
 	}
 
 	var b1 = A(1);
 	var b2 = A(2);
 
 	print('b1() =', b1());
 	print('b2() =', b2());
 	print('b1', b1 == b2 ? '==' : '!=', 'b2');
 
 }.call();
 
 =================
 Result of the browsers, rhino, jshost(SpiderMonkey):
 
 b1() = 1
 b2() = 4
 b1 != b2
 
 Result of DMDScript:
 
 Digital Mars DMDScript 1.13
 www.digitalmars.com
 Compiled by Digital Mars DMD D compiler
 Copyright (c) 1999-2007 by Digital Mars
 written by Walter Bright
 1 source files
 
 b1() = NaN
 b2() = NaN
 b1 == b2
 
 
 BTW, DMDScript doesn't allow definitely correct function calling syntax:
 
 (function (s) { print(s) })('Hello world')
 void function (s) { print(s) } ('Hello world')
 
 So I must write
 
 void function () {...}.call()
 

 `
 end

Aug 01 2007