Example #1
0
 def transform_handlers(self, registry, blocks):
     result = {}
     for (path, name), (args, code) in blocks.iteritems():
         code = process(code, self, registry, args)
         code = "function(%s) %s" % (",".join(args), code)
         result.setdefault(code, []).append((path, name))
     return sorted(result.iteritems())
Example #2
0
	def transform_handlers(self, registry, blocks):
		result = OrderedDict()
		for (path, name), (args, code, async_) in blocks.items():
			if name == '__complete':
				code = code.strip()
				if code[0] == '{' and code[-1] == '}':
					code = '{ @super.__complete.call(this)\n' + code[1:-1].strip() + '\n}'
			code = process(code, self, registry, args)
			code = "%sfunction(%s) %s" %("async " if async_ else "",  ",".join(args), code)
			result.setdefault(code, []).append((path, name))
		return sorted(result.items())
Example #3
0
	def generate_setup_code(self, registry, parent, closure, ident_n = 1):
		r = []
		ident = "\t" * ident_n

		for target, value in self.assignments.iteritems():
			if target == "id":
				continue
			t = type(value)
			#print self.name, target, value
			target_lvalue = self.get_lvalue(parent, target)
			if t is str:
				value = replace_enums(value, self, registry)
				deps = parse_deps(parent, value)
				if deps:
					var = "update$%s$%s" %(escape(parent), escape(target))
					r.append('//assigning %s to %s' %(target, value))
					r.append("%svar %s = (function() { %s = (%s); }).bind(%s)" %(ident, var, target_lvalue, value, parent))
					undep = []
					for idx, _dep in enumerate(deps):
						path, dep = _dep
						depvar = "dep$%s$%s$%d" %(escape(parent), escape(target), idx)
						r.append('%svar %s = %s' %(ident, depvar, path))
						r.append("%s%s.connectOnChanged(%s, '%s', %s)" %(ident, parent, depvar, dep, var))
						undep.append("[%s, '%s', %s]" %(depvar, dep, var))
					r.append("%s%s._removeUpdater('%s', [%s])" %(ident, parent, target, ",".join(undep)))
					r.append("%s%s();" %(ident, var))
				else:
					r.append('//assigning %s to %s' %(target, value))
					r.append("%s%s._removeUpdater('%s'); %s = (%s);" %(ident, parent, target, target_lvalue, value))

			elif t is component_generator:
				if target == "delegate":
					continue
				var = "%s$%s" %(escape(parent), escape(target))
				r.append(self.call_setup(registry, ident_n, var, value, closure))
			else:
				raise Exception("skip assignment %s = %s" %(target, value))

		if not self.prototype:
			for _name, argscode in self.methods.iteritems():
				path, name = _name
				args, code = argscode
				path = path_or_parent(path, parent)
				code = process(code, self, registry)
				r.append("%s%s.%s = (function(%s) %s ).bind(%s)" %(ident, path, name, ",".join(args), code, path))

		for _name, argscode in self.signal_handlers.iteritems():
			path, name = _name
			if not path and self.prototype and name != 'completed': #sync with condition above
				continue
			args, code = argscode
			code = process(code, self, registry)
			path = path_or_parent(path, parent)
			if name != "completed":
				r.append("%s%s.on('%s', (function(%s) %s ).bind(%s))" %(ident, path, name, ",".join(args), code, path))
			else:
				r.append("%s%s._context._onCompleted((function() %s ).bind(%s))" %(ident, path, code, path))

		for _name, code in self.changed_handlers.iteritems():
			path, name = _name
			if not path and self.prototype: #sync with condition above
				continue
			code = process(code, self, registry)
			path = path_or_parent(path, parent)
			r.append("%s%s.onChanged('%s', (function(value) %s ).bind(%s))" %(ident, path, name, code, path))

		for _name, code in self.key_handlers.iteritems():
			path, name = _name
			if not path and self.prototype: #sync with condition above
				continue
			code = process(code, self, registry)
			path = path_or_parent(path, parent)
			r.append("%s%s.onPressed('%s', (function(key, event) %s ).bind(%s))" %(ident, path, name, code, path))

		r.append(self.generate_animations(registry, parent))

		for idx, value in enumerate(self.children):
			var = '%s$child%d' %(escape(parent), idx)
			r.append(self.call_setup(registry, ident_n, var, value, closure))
			r.append("%s%s.addChild(%s)" %(ident, parent, var));

		if self.elements:
			r.append("\t%s.assign(%s)" %(parent, json.dumps(self.elements)))

		return "\n".join(r)
Example #4
0
	def generate_prototype(self, registry, ident_n = 1):
		assert self.prototype == True

		#HACK HACK: make immutable
		registry.id_set = set(['context'])
		self.collect_id(registry.id_set)

		r = []
		ident = "\t" * ident_n

		base_type = self.get_base_type(registry)

		r.append("%svar %s = %s.prototype = Object.create(%s)\n" %(ident, self.proto_name, self.local_name, self.base_proto_name))
		r.append("%s%s.constructor = %s\n" %(ident, self.proto_name, self.local_name))

		r.append("%s%s.componentName = '%s'" %(ident, self.proto_name, self.name))

		for name in self.signals:
			r.append("%s%s.%s = _globals.core.createSignal('%s')" %(ident, self.proto_name, name, name))

		for _name, argscode in self.methods.iteritems():
			path, name = _name
			if path:
				raise Exception('no <id> qualifiers (%s) allowed in prototypes %s (%s)' %(path, name, self.name))
			args, code = argscode
			code = process(code, self, registry)
			r.append("%s%s.%s = function(%s) %s" %(ident, self.proto_name, name, ",".join(args), code))

		for prop in self.properties:
			for name, default_value in prop.properties:
				args = ["%s" %self.proto_name, "'%s'" %prop.type, "'%s'" %name]
				if lang.value_is_trivial(default_value):
					args.append(default_value)
				r.append("%score.addProperty(%s)" %(ident, ", ".join(args)))

		for name, prop in self.enums.iteritems():
			values = prop.values

			for i in xrange(0, len(values)):
				r.append("/** @const @type {number} */")
				r.append("%s%s.%s = %d" %(ident, self.proto_name, values[i], i))
				r.append("/** @const @type {number} */")
				r.append("%s%s.%s = %d" %(ident, self.local_name, values[i], i))

			args = [self.proto_name, "'enum'", "'%s'" %name]
			if prop.default is not None:
				args.append("%s.%s" %(self.local_name, prop.default))
			r.append("%score.addProperty(%s)" %(ident, ", ".join(args)))

		for _name, code in self.changed_handlers.iteritems():
			path, name = _name
			if path or not self.prototype: #sync with condition below
				continue

			assert not path
			code = process(code, self, registry)
			r.append("%s_globals.core._protoOnChanged(%s, '%s', (function(value) %s ))" %(ident, self.proto_name, name, code))

		for _name, argscode in self.signal_handlers.iteritems():
			path, name = _name
			if path or not self.prototype or name == 'completed': #sync with condition below
				continue
			args, code = argscode
			code = process(code, self, registry)
			r.append("%s_globals.core._protoOn(%s, '%s', (function(%s) %s ))" %(ident, self.proto_name, name, ", ".join(args), code))

		for _name, code in self.key_handlers.iteritems():
			path, name = _name
			if path or not self.prototype: #sync with condition below
				continue
			code = process(code, self, registry)
			r.append("%s_globals.core._protoOnKey(%s, '%s', (function(key, event) %s ))" %(ident, self.proto_name, name, code))


		generate = False

		code = self.generate_creators(registry, 'this', '__closure', ident_n + 1).strip()
		if code:
			generate = True
		b = '\t%s%s.__create.call(this, __closure.__base = { })' %(ident, self.base_proto_name)
		code = '%s%s.__create = function(__closure) {\n%s\n%s\n%s}' \
			%(ident, self.proto_name, b, code, ident)

		setup_code = self.generate_setup_code(registry, 'this', '__closure', ident_n + 2).strip()
		b = '%s%s.__setup.call(this, __closure.__base); delete __closure.__base' %(ident, self.base_proto_name)
		if setup_code:
			generate = True
		setup_code = '%s%s.__setup = function(__closure) {\n%s\n%s\n}' \
			%(ident, self.proto_name, b, setup_code)

		if generate:
			r.append('')
			r.append(code)
			r.append(setup_code)
			r.append('')

		return "\n".join(r)