//! (static)
std::pair<bool, ObjRef> loadAndExecute(Runtime & runtime, const std::string & filename) {
	ObjRef script;
	try {
		script = loadScriptFile(filename);
	} catch (Exception * error) {
		std::cerr << "\nError occurred while loading file '" << filename << "':\n" << error->toString() << std::endl;
		return std::make_pair(false, error);
	}
	bool success = true;
	ObjRef result;
	try {
		runtime.executeObj(script.get());
		result = runtime.getResult();
		if(runtime.getState() == Runtime::STATE_EXCEPTION) {
			std::cout << "\nException caught (1):\n" << result.toString() << std::endl;
			success = false;
		}
	} catch (Object * o) {
		result = o;
		std::cout << "\nException caught (2):\n" << result.toString() << std::endl;
		success = false;
	} catch (...) {
		std::cout << "\nCaught unknown C++ exception." << std::endl;
		success = false;
	}
	return std::make_pair(success, result);
}
/*! Tries to locate the given __filename__ with the current searchPath set in the runtime.
	@return the path to the file or the original __filename__ if the file could not be found.	*/
static std::string findFile(Runtime & runtime, const std::string & filename){
	static const identifierId seachPathsId=stringToIdentifierId("__searchPaths");

	std::string file(FileUtils::condensePath(filename));
	if( FileUtils::isFile(file)!=1 ){
		if(Array * searchPaths = dynamic_cast<Array*>(runtime.getAttribute(seachPathsId))){
			for(ERef<Iterator> itRef=searchPaths->getIterator();!itRef->end();itRef->next()){
				ObjRef valueRef = itRef->value();
				std::string s(FileUtils::condensePath(valueRef.toString()+"/"+filename));
				if( FileUtils::isFile(s)==1 ){
					file = s;
					break;
				}
			}
		}
	}
	return file;
}
/*! Tries to locate the given __filename__ with the current searchPath set in the runtime.
	@return the path to the file or the original __filename__ if the file could not be found.	*/
static std::string findFile(Runtime & runtime, const std::string & filename){
	static const StringId seachPathsId("__searchPaths");

	std::string file(IO::condensePath(filename));
	if( IO::getEntryType(file)!=IO::TYPE_FILE ){
		if(Array * searchPaths = dynamic_cast<Array*>(runtime.getAttribute(seachPathsId).getValue())){
			for(ERef<Iterator> itRef=searchPaths->getIterator();!itRef->end();itRef->next()){
				ObjRef valueRef = itRef->value();
				std::string s(IO::condensePath(valueRef.toString()+'/'+filename));
				if( IO::getEntryType(s)==IO::TYPE_FILE ){
					file = s;
					break;
				}
			}
		}
	}
	return file;
}
Exemple #4
0
			Util::GenericAttribute * convertFromEScript(const EScript::ObjPtr & object) override {
				// is a Number?
				EScript::Number * n = dynamic_cast<EScript::Number *> (object.get());
				if (n != nullptr) {
					return GenericAttribute::createNumber(n->getValue());
				}

				// is a Bool?
				EScript::Bool * b = dynamic_cast<EScript::Bool *> (object.get());
				if (b != nullptr) {
					return GenericAttribute::createBool(b->toBool());
				}

				// is a String?
				EScript::String * s = dynamic_cast<EScript::String *> (object.get());
				if (s != nullptr) {
					return GenericAttribute::createString(s->getString());
				}

				// is an Array?
				EScript::Array * a = dynamic_cast<EScript::Array *> (object.get());
				if (a != nullptr) {
					auto gl = new Util::GenericAttributeList;
					for (ERef<EScript::Iterator> i = a->getIterator(); !i->end(); i->next()) {
						ObjRef value = i->value();
						gl->push_back(convertEScriptObjectToGenericAttribute(value));
					}
					return gl;
				}
				// is a Map?
				EScript::Map * m = dynamic_cast<EScript::Map *> (object.get());
				if (m != nullptr) {
					auto gm = new Util::GenericAttributeMap;
					for (ERef<EScript::Iterator> i = m->getIterator(); !i->end(); i->next()) {
						ObjRef key = i->key();
						ObjRef value = i->value();
						gm->setValue(key->toString(), convertEScriptObjectToGenericAttribute(value));
					}
					return gm;
				}

				return nullptr;
			}
int main(int argc,char * argv[]) {

    EScript::init();

#ifdef ES_DEBUG_MEMORY
	Tokenizer::identifyStaticToken(0); // init constants
    Debug::clearObjects();
#endif
    ERef<Script> s=new Script();
    ERef<Runtime> rt=new Runtime();

    rt->initVariable("args",Array::create(argc,argv));

     // --- Test NumberRef
    int numberRefTest1=17;
    rt->initVariable("numberRefTest1",new NumberRef(numberRefTest1));
    float numberRefTest2=17;
    rt->initVariable("numberRefTest2",new NumberRef(numberRefTest2));


    std::string file="tests/test.escript";
    if(argc>1) {
        file=argv[1];
    }
    try {
        s->loadScript(file);
    } catch (Object * e) {
        std::cerr << "\n\a "<<e->toString() << "\n"<<std::endl;
        return EXIT_FAILURE;
    }

    ObjRef result;
    try {
        result=rt->executeObj(s.get());
    }catch (Exception * e) {
        std::cout << "\nERROR \a";
        if (e) std::cout << e->toString();
    }catch (Object * o) {
        std::cout << "\nCaught: ";
        if (o) std::cout << o->toString();
    }catch (...) {
        std::cout << "\nCaught unknown C++ expception. ";
    }

    std::cout << "\n --- \n\n\n";

//    // --- Test NumberRef
//    std::cout << numberRefTest1<<"," <<numberRefTest2; // should be (18,16)

    if (!result.isNull()) {
        std::cout << "\nResult: " << result.toString()<<"\n";
    }
    s=NULL;
    rt=NULL;

    #ifdef ES_DEBUG_MEMORY
//        std::cout << "\nGLOBALS: " << Script::SGLOBALS<<"\n";

        Debug::showObjects();
    #endif
//system("pause");
    return EXIT_SUCCESS;
}