Пример #1
0
//! init
void IOLib::init(EScript::Namespace * o) {
	Namespace * lib = new Namespace;
	declareConstant(o,"IO",lib);

	//! [ESF] string fileGetContents(string filename,[format])
	ES_FUNCTION_DECLARE(lib,"fileGetContents",1,2, {

		StringData content;
		try{
			content = IO::loadFile(parameter[0].toString());
		}catch(const std::ios::failure & e){
			runtime.setException(e.what());
			return nullptr;
		}
		if(parameter.count()>1) {
			if(parameter[1].toString()=="UTF16LE") {
				const std::string contentS(StringUtils::UCS2LE_to_ANSII(content.str()));
				return contentS;
			} else {
				runtime.setException("Unknown format");
				return nullptr;
			}
		}
		return create(content);
	})
//! (static)
std::pair<bool, ObjRef> eval(Runtime & runtime, const StringData & code,const StringId & fileId) {
	try {
		ObjRef result = _eval(runtime,CodeFragment( (fileId.empty() ? Consts::FILENAME_INLINE : fileId), code));
		return std::make_pair(true,std::move(result));
	} catch (Object * error) {
		std::ostringstream os;
		os << "Error occurred while evaluating '" << code.str() << "':\n" << error->toString();
		runtime.log(Logger::LOG_ERROR,os.str());
		return std::make_pair(false, error);
	}
}