Beispiel #1
0
Variable AVM::createNewObject(const Common::UString &name, std::vector<Variable> arguments) {
	Variable variable = getVariable(name);

	if (!variable.isFunction())
		throw Common::Exception("Object is not a function");

	if (!variable.asObject()->hasMember("prototype"))
		throw Common::Exception("Object has no prototype");

	ObjectPtr newObject(new Object(variable.asObject()->getMember("prototype").asObject().get()));
	Function *constructor = dynamic_cast<Function *>(variable.asObject().get());

	if (!constructor)
		throw Common::Exception("Constructor is not a function");

	byte counter = 1;
	if (constructor->getPreloadRootFlag()) {
		storeRegister(_variables["_root"], counter);
		counter += 1;
	}
	if (constructor->getPreloadThisFlag()) {
		storeRegister(Variable(newObject), counter);
		counter += 1;
	}
	if (constructor->getPreloadSuperFlag()) {
		storeRegister(Variable(newObject->getMember("constructor")), counter);
		counter += 1;
	}

	for (size_t i = 0; i < arguments.size(); ++i) {
		storeRegister(arguments[i], counter);
		counter += 1;
	}

	(*constructor)(*this);

	return Variable(newObject);
}
Beispiel #2
0
Variable AVM::registerClass(AVM &avm) {
	Variable name = avm.getRegister(1);
	Variable object = avm.getRegister(2);

	if (!name.isString())
		throw Common::Exception("AVM::registerClass(): name is not a string");
	if (!object.isObject())
		throw Common::Exception("AVM::registerClass(): value is not an object");

	if (_registerClass)
		_registerClass(name.asString(), object.asObject());

	return Variable();
}