Beispiel #1
0
	def generate_creators(self, registry, parent, closure, ident_n = 1):
		r = []
		ident = "\t" * ident_n

		if not self.prototype:
			for name in self.signals:
				r.append("%s%s.%s = _globals.core.createSignal('%s').bind(%s)" %(ident, parent, name, name, parent))

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

			for name, prop in self.enums.iteritems():
				raise Exception('adding enums in runtime is unsupported, consider putting this property (%s) in prototype' %name)

		for idx, gen in enumerate(self.children):
			var = "%s$child%d" %(escape(parent), idx)
			component = registry.find_component(self.package, gen.component.name)
			r.append("%svar %s = new _globals.%s(%s)" %(ident, var, component, parent))
			r.append("%s%s.%s = %s" %(ident, closure, var, var))
			code = self.call_create(registry, ident_n, var, gen, closure)
			r.append(code)

		for target, value in self.assignments.iteritems():
			if target == "id":
				if "." in value:
					raise Exception("expected identifier, not expression")
				r.append("%s%s._setId('%s')" %(ident, parent, value))
			elif target.endswith(".id"):
				raise Exception("setting id of the remote object is prohibited")
			else:
				self.check_target_property(registry, target)

			if isinstance(value, component_generator):
				if target != "delegate":
					var = "%s$%s" %(escape(parent), escape(target))
					r.append("//creating component %s" %value.name)
					r.append("%svar %s = new _globals.%s(%s)" %(ident, var, registry.find_component(value.package, value.component.name), parent))
					r.append("%s%s.%s = %s" %(ident, closure, var, var))
					code = self.call_create(registry, ident_n, var, value, closure)
					r.append(code)
					r.append('%s%s.%s = %s' %(ident, parent, target, var))
				else:
					code = "%svar delegate = new _globals.%s(%s, true)\n" %(ident, registry.find_component(value.package, value.component.name), parent)
					code += "%svar __closure = { delegate: delegate }\n" %(ident)
					code += self.call_create(registry, ident_n + 1, 'delegate', value, '__closure') + '\n'
					code += self.call_setup(registry, ident_n + 1, 'delegate', value, '__closure') + '\n'
					r.append("%s%s.%s = (function() {\n%s\n%sreturn delegate\n}).bind(%s)" %(ident, parent, target, code, ident, parent))

		for name, target in self.aliases.iteritems():
			get, pname = generate_accessors(target)
			r.append("%score.addAliasProperty(%s, '%s', (function() { return %s; }).bind(%s), '%s')" \
				%(ident, parent, name, get, parent, pname))

		return "\n".join(r)
Beispiel #2
0
    def generate_creators(self, registry, parent, closure, ident_n=1):
        r = []
        ident = "\t" * ident_n

        if not self.prototype:
            for name in self.signals:
                r.append(
                    "%s%s.%s = _globals.core.createSignal('%s').bind(%s)" %
                    (ident, parent, name, name, parent))

            for prop in self.properties:
                for name, default_value in prop.properties:
                    if prop.lazy:
                        gen = self.lazy_properties[name]
                        r.append(
                            self.generate_lazy_property(
                                registry, parent, prop.type, name, gen,
                                ident_n))
                    elif prop.const:
                        r.append(
                            self.generate_const_property(
                                registry, parent, name,
                                self.const_properties[name]))
                    else:
                        args = [parent, "'%s'" % prop.type, "'%s'" % name]
                        if lang.value_is_trivial(default_value):
                            default_value, deps = parse_deps(
                                '@error', default_value,
                                partial(self.transform_root, registry))
                            if deps:
                                raise Exception(
                                    'trivial value emits dependencies')
                            args.append(default_value)
                        r.append("\tcore.addProperty(%s)" % (", ".join(args)))

            for name, prop in self.enums.iteritems():
                raise Exception(
                    'adding enums in runtime is unsupported, consider putting this property (%s) in prototype'
                    % name)

        for idx, gen in enumerate(self.children):
            var = "%s$child%d" % (escape(parent), idx)
            component = registry.find_component(self.package,
                                                gen.component.name)
            r.append("%svar %s = new _globals.%s(%s)" %
                     (ident, var, component, parent))
            r.append("%s%s.%s = %s" % (ident, closure, var, var))
            code = self.call_create(registry, ident_n, var, gen, closure)
            r.append(code)
            r.append("%s%s.addChild(%s)" % (ident, parent, var))

        for target, value in self.assignments.iteritems():
            if target == "id":
                if "." in value:
                    raise Exception("expected identifier, not expression")
                r.append("%s%s._setId('%s')" % (ident, parent, value))
            elif target.endswith(".id"):
                raise Exception(
                    "setting id of the remote object is prohibited")
            else:
                self.check_target_property(registry, target)

            if isinstance(value, component_generator):
                if target != "delegate":
                    var = "%s$%s" % (escape(parent), escape(target))
                    r.append("//creating component %s" % value.name)
                    r.append(
                        "%svar %s = new _globals.%s(%s)" %
                        (ident, var,
                         registry.find_component(
                             value.package, value.component.name), parent))
                    r.append("%s%s.%s = %s" % (ident, closure, var, var))
                    code = self.call_create(registry, ident_n, var, value,
                                            closure)
                    r.append(code)
                    r.append('%s%s.%s = %s' % (ident, parent, target, var))
                else:
                    code = self.generate_creator_function(
                        registry, 'delegate', value, ident_n)
                    r.append("%s%s.%s = %s" % (ident, parent, target, code))

        for name, target in self.aliases.iteritems():
            get, pname = generate_accessors(
                parent, target, partial(self.transform_root, registry))
            r.append("%score.addAliasProperty(%s, '%s', function() { return %s }, '%s')" \
             %(ident, parent, name, get, pname))

        return "\n".join(r)