예제 #1
0
static void scArrayPush(const CFunctionsScopePtr &c, void *data) {
	CScriptVarPtr arr = c->getArgument("this");
	int l = arr->getArrayLength();
	int args = c->getArgumentsLength();
	for (int i=0;i<args;i++)
		arr->setArrayIndex(l+i, c->getArgument(i));
}
예제 #2
0
static void scArraySort(const CFunctionsScopePtr &c, void *data) {
	CScriptVarPtr arr = c->getArgument("this");
	CScriptVarFunctionPtr cmp_fnc; 
	uint32_t len = arr->getLength();
	if(len > 1) {
		int args = c->getArgumentsLength();
		if(args) {
			cmp_fnc = c->getArgument(0);
			if(!cmp_fnc) c->throwError(TypeError, "invalid Array.prototype.sort argument");
		}
		SCRIPTVAR_CHILDS_it begin = lower_bound(arr->Childs.begin(), arr->Childs.end(), "0");
		/* in ECMAScript the sort algorithm is not specified
		 * in some cases sort throws a TypeError e.G. an array element is read-only, non-configurable, getter or setter
		 * this will result in a partially sorted array 
		 * in worst case the sort algorithm results in an endless loop (more getters with constant return value)
		 *
		 * 42TinyJS checks before sort if an element read-only, setter or getter and throws immediately a TypeError
		 */
		for(SCRIPTVAR_CHILDS_it it = begin; it != arr->Childs.end(); ++it) {
			if(!(*it)->isWritable()) c->throwError(TypeError, "property "+(*it)->getName()+" is read-only");
			if(!(*it)->isConfigurable()) c->throwError(TypeError, "property "+(*it)->getName()+" is non-configurable");
			if(!(*it)->getVarPtr()->isAccessor()) c->throwError(TypeError, "property "+(*it)->getName()+" is a getter and/or a setter");
		}
		sort(begin, arr->Childs.end(), ::cmp_fnc(c, cmp_fnc));
		uint32_t idx = 0;
		for(SCRIPTVAR_CHILDS_it it=begin; it != arr->Childs.end(); ++it, ++idx)
			(*it)->reName(int2string(idx));
		sort(begin, arr->Childs.end(), cmp_by_name);
	}
	c->setReturnVar(arr);
}
예제 #3
0
static void scArrayPop(const CFunctionsScopePtr &c, void *data) {
	CScriptVarPtr arr = c->getArgument("this");
	uint32_t len = c->getLength(arr);
	if(len) {
		c->setReturnVar(c->getPropertyValue(arr, len-1));
		arr->removeChild(int2string(len-1));
	} else
		c->setReturnVar(c->constScriptVar(Undefined));
}
예제 #4
0
static void scArrayJoin(const CFunctionsScopePtr &c, void *data) {
	string sep = c->getArgument("separator")->toString();
	CScriptVarPtr arr = c->getArgument("this");

	ostringstream sstr;
	int l = arr->getArrayLength();
	for (int i=0;i<l;i++) {
		if (i>0) sstr << sep;
		sstr << arr->getArrayIndex(i)->toString();
	}

	c->setReturnVar(c->newScriptVar(sstr.str()));
}
예제 #5
0
static void scArrayRemove(const CFunctionsScopePtr &c, void *data) {
	CScriptVarPtr obj = c->getArgument("obj");
	CScriptVarPtr arr = c->getArgument("this");
	uint32_t l = arr->getLength();
	uint32_t offset = 0;
	for(SCRIPTVAR_CHILDS_it it= lower_bound(arr->Childs.begin(), arr->Childs.end(), "0"); it != arr->Childs.end(); ++it) {
		while(it != arr->Childs.end() && obj->mathsOp(it->getter(), LEX_EQUAL)->toBoolean()) {
			it = arr->Childs.erase(it);
			offset++;
		}
		if(offset && it != arr->Childs.end())
			(*it)->reName(int2string((uint32_t)strtoul((*it)->getName().c_str(), 0, 10)-offset) );
	}
}
예제 #6
0
//************************************
// Method:    getRegExpData
// FullName:  getRegExpData
// Access:    public static 
// Returns:   bool true if regexp-param=RegExp-Object / other false
// Qualifier:
// Parameter: const CFunctionsScopePtr & c
// Parameter: const string & regexp - parameter name of the regexp
// Parameter: bool noUndefined - true an undefined regexp aims in "" else in "undefined"
// Parameter: const string & flags - parameter name of the flags
// Parameter: string & substr - rgexp.source
// Parameter: bool & global
// Parameter: bool & ignoreCase
// Parameter: bool & sticky
//************************************
static CScriptVarPtr getRegExpData(const CFunctionsScopePtr &c, const string &regexp, bool noUndefined, const char *flags_argument, string &substr, bool &global, bool &ignoreCase, bool &sticky) {
	CScriptVarPtr regexpVar = c->getArgument(regexp);
	if(regexpVar->isRegExp()) {
#ifndef NO_REGEXP
		CScriptVarRegExpPtr RegExp(regexpVar);
		substr = RegExp->Regexp();
		ignoreCase = RegExp->IgnoreCase();
		global = RegExp->Global();
		sticky = RegExp->Sticky();
		return RegExp;
#endif /* NO_REGEXP */
	} else {
		substr.clear();
		if(!noUndefined || !regexpVar->isUndefined()) substr = regexpVar->toString();
		CScriptVarPtr flagVar;
		if(flags_argument && (flagVar = c->getArgument(flags_argument)) && !flagVar->isUndefined()) {
			string flags = flagVar->toString();
			string::size_type pos = flags.find_first_not_of("gimy");
			if(pos != string::npos) {
				c->throwError(SyntaxError, string("invalid regular expression flag ")+flags[pos]);
			}
			global = flags.find_first_of('g')!=string::npos;
			ignoreCase = flags.find_first_of('i')!=string::npos;
			sticky = flags.find_first_of('y')!=string::npos;
		} else
			global = ignoreCase = sticky = false;
	}
	return CScriptVarPtr();
}
예제 #7
0
static void scStringReplace(const CFunctionsScopePtr &c, void *) {
	const string str = this2string(c);
	CScriptVarPtr newsubstrVar = c->getArgument("newsubstr");
	string substr, ret_str;
	bool global, ignoreCase, sticky;
	bool isRegExp = getRegExpData(c, "substr", false, "flags", substr, global, ignoreCase, sticky);
	if(isRegExp && !newsubstrVar->isFunction()) {
#ifndef NO_REGEXP
		regex::flag_type flags = regex_constants::ECMAScript;
		if(ignoreCase) flags |= regex_constants::icase;
		regex_constants::match_flag_type mflags = regex_constants::match_default;
		if(!global) mflags |= regex_constants::format_first_only;
		if(sticky) mflags |= regex_constants::match_continuous;
		ret_str = regex_replace(str, regex(substr, flags), newsubstrVar->toString(), mflags);
#endif /* NO_REGEXP */
	} else {
		bool (*search)(const string &, const string::const_iterator &, const string &, bool, bool, string::const_iterator &, string::const_iterator &);
#ifndef NO_REGEXP
		if(isRegExp) 
			search = regex_search;
		else
#endif /* NO_REGEXP */
			search = string_search;
		string newsubstr;
		vector<CScriptVarPtr> arguments;
		if(!newsubstrVar->isFunction()) 
			newsubstr = newsubstrVar->toString();
		global = global && substr.length();
		string::const_iterator search_begin=str.begin(), match_begin, match_end;
		if(search(str, search_begin, substr, ignoreCase, sticky, match_begin, match_end)) {
			do {
				ret_str.append(search_begin, match_begin);
				if(newsubstrVar->isFunction()) {
					arguments.push_back(c->newScriptVar(string(match_begin, match_end)));
					newsubstr = c->getContext()->callFunction(newsubstrVar, arguments)->toString();
					arguments.pop_back();
				}
				ret_str.append(newsubstr);
#if 1 /* Fix from "vcmpeq" (see Issue 14) currently untested */
				if (match_begin == match_end) {
					if (search_begin != str.end())
						++search_begin;
					else
						break;
				} else {
					search_begin = match_end;
				}
#else
				search_begin = match_end;
#endif
			} while(global && search(str, search_begin, substr, ignoreCase, sticky, match_begin, match_end));
		}
		ret_str.append(search_begin, str.end());
	}
	c->setReturnVar(c->newScriptVar(ret_str));
}
예제 #8
0
static void scArrayContains(const CFunctionsScopePtr &c, void *data) {
	CScriptVarPtr obj = c->getArgument("obj");
	CScriptVarPtr arr = c->getArgument("this");

	int l = arr->getArrayLength();
	CScriptVarPtr equal = c->constScriptVar(Undefined);
	for (int i=0;i<l;i++) {
		equal = obj->mathsOp(arr->getArrayIndex(i), LEX_EQUAL);
		if(equal->toBoolean()) {
			c->setReturnVar(c->constScriptVar(true));
			return;
		}
	}
	c->setReturnVar(c->constScriptVar(false));
}
예제 #9
0
static void scArrayFill(const CFunctionsScopePtr &c, void *data) {
	CScriptVarPtr arr = c->getArgument("this");
	uint32_t len = c->getLength(arr);
	CNumber startNumber=0, endNumber=len;
	CScriptVarPtr value = c->getArgument(0);
	CScriptVarPtr startVar = c->getArgument(1);
	CScriptVarPtr endVar = c->getArgument(2);
	c->setReturnVar(arr);
	if(!startVar->isUndefined()) startNumber = startVar->toNumber();
	if(!endVar->isUndefined()) endNumber = endVar->toNumber();
	uint32_t start = (startNumber.sign()<0 ? startNumber+len : startNumber).clamp(0, len).toUInt32();
	uint32_t end = (endNumber.sign()<0 ? endNumber+len : endNumber).clamp(0, len).toUInt32();
	for(; start < end; ++start) c->setProperty(arr, start, value);
}
예제 #10
0
static void scArrayRemove(const CFunctionsScopePtr &c, void *data) {
	CScriptVarPtr obj = c->getArgument("obj");
	CScriptVarPtr arr = c->getArgument("this");
	int i;
	vector<int> removedIndices;

	int l = arr->getArrayLength();
	CScriptVarPtr equal = c->constScriptVar(Undefined);
	for (i=0;i<l;i++) {
		equal = obj->mathsOp(arr->getArrayIndex(i), LEX_EQUAL);
		if(equal->toBoolean()) {
			removedIndices.push_back(i);
		}
	}
	if(removedIndices.size()) {
		vector<int>::iterator remove_it = removedIndices.begin();
		int next_remove = *remove_it;
		int next_insert = *remove_it++;
		for (i=next_remove;i<l;i++) {

			CScriptVarLinkPtr link = arr->findChild(int2string(i));
			if(i == next_remove) {
				if(link) arr->removeLink(link);
				if(remove_it != removedIndices.end())
					next_remove = *remove_it++;
			} else {
				if(link) {
					arr->setArrayIndex(next_insert++, link);
					arr->removeLink(link);
				}	
			}
		}
	}
}
예제 #11
0
static void scObjectClone(const CFunctionsScopePtr &c, void *) {
	CScriptVarPtr obj = c->getArgument("this");
	c->setReturnVar(obj->clone());
}
예제 #12
0
static string this2string(const CFunctionsScopePtr &c) {
	CScriptVarPtr This = c->getArgument("this");
	CheckObjectCoercible(This);
	return This->toString();
}