Skip to content

Instantly share code, notes, and snippets.

@BenjaminPoulain
Created April 4, 2012 22:24
Show Gist options
  • Save BenjaminPoulain/2306122 to your computer and use it in GitHub Desktop.
Save BenjaminPoulain/2306122 to your computer and use it in GitHub Desktop.
diff --git a/Source/JavaScriptCore/runtime/ArrayPrototype.cpp b/Source/JavaScriptCore/runtime/ArrayPrototype.cpp
index 818895c..4b1278a 100644
--- a/Source/JavaScriptCore/runtime/ArrayPrototype.cpp
+++ b/Source/JavaScriptCore/runtime/ArrayPrototype.cpp
@@ -383,7 +383,7 @@ EncodedJSValue JSC_HOST_CALL arrayProtoFuncJoin(ExecState* exec)
UString separator;
if (!exec->argument(0).isUndefined())
- separator = exec->argument(0).toString(exec)->value(exec);
+ separator = fastJSValuetoUString(exec->argument(0), exec);
if (separator.isNull())
separator = UString(",");
@@ -399,7 +399,7 @@ EncodedJSValue JSC_HOST_CALL arrayProtoFuncJoin(ExecState* exec)
JSValue element = array->getIndex(k);
if (!element.isUndefinedOrNull())
- stringJoiner.append(element.toString(exec)->value(exec));
+ stringJoiner.append(fastJSValuetoUString(element, exec));
else
stringJoiner.append(UString());
}
@@ -408,7 +408,7 @@ EncodedJSValue JSC_HOST_CALL arrayProtoFuncJoin(ExecState* exec)
for (; k < length; k++) {
JSValue element = thisObj->get(exec, k);
if (!element.isUndefinedOrNull())
- stringJoiner.append(element.toString(exec)->value(exec));
+ stringJoiner.append(fastJSValuetoUString(element, exec));
else
stringJoiner.append(UString());
}
diff --git a/Source/JavaScriptCore/runtime/JSString.h b/Source/JavaScriptCore/runtime/JSString.h
index 32a3278..7c21397 100644
--- a/Source/JavaScriptCore/runtime/JSString.h
+++ b/Source/JavaScriptCore/runtime/JSString.h
@@ -462,6 +462,35 @@ namespace JSC {
return toStringSlowCase(exec);
}
+ inline UString JSValue::toUString(ExecState* exec) const
+ {
+ if (isString())
+ return static_cast<JSString*>(asCell())->value(exec);
+ return toUStringSlowCase(exec);
+ }
+
+ ALWAYS_INLINE UString fastJSValueNotStringtoUString(const JSValue& value, ExecState* exec)
+ {
+ JSGlobalData& globalData = exec->globalData();
+ if (value.isInt32())
+ return globalData.numericStrings.add(value.asInt32());
+ if (value.isDouble())
+ return globalData.numericStrings.add(value.asDouble());
+ if (value.isTrue())
+ return globalData.propertyNames->trueKeyword.ustring();
+ if (value.isFalse())
+ return globalData.propertyNames->falseKeyword.ustring();
+ return value.toString(exec)->value(exec);
+ }
+
+ ALWAYS_INLINE UString fastJSValuetoUString(const JSValue& value, ExecState* exec)
+ {
+ if (value.isString())
+ return static_cast<JSString*>(value.asCell())->value(exec);
+
+ return fastJSValueNotStringtoUString(value, exec);
+ }
+
} // namespace JSC
#endif // JSString_h
diff --git a/Source/JavaScriptCore/runtime/JSValue.cpp b/Source/JavaScriptCore/runtime/JSValue.cpp
index 7bb895a..fa83c6e 100644
--- a/Source/JavaScriptCore/runtime/JSValue.cpp
+++ b/Source/JavaScriptCore/runtime/JSValue.cpp
@@ -283,4 +283,9 @@ JSString* JSValue::toStringSlowCase(ExecState* exec) const
return value.toString(exec);
}
+UString JSValue::toUStringSlowCase(ExecState* exec) const
+{
+ return fastJSValueNotStringtoUString(*this, exec);
+}
+
} // namespace JSC
diff --git a/Source/JavaScriptCore/runtime/JSValue.h b/Source/JavaScriptCore/runtime/JSValue.h
index ba0ae23..006e894 100644
--- a/Source/JavaScriptCore/runtime/JSValue.h
+++ b/Source/JavaScriptCore/runtime/JSValue.h
@@ -203,6 +203,7 @@ namespace JSC {
// been set in the ExecState already.
double toNumber(ExecState*) const;
JSString* toString(ExecState*) const;
+ UString toUString(ExecState*) const;
JSObject* toObject(ExecState*) const;
JSObject* toObject(ExecState*, JSGlobalObject*) const;
@@ -251,6 +252,7 @@ namespace JSC {
inline const JSValue asValue() const { return *this; }
JS_EXPORT_PRIVATE double toNumberSlowCase(ExecState*) const;
JS_EXPORT_PRIVATE JSString* toStringSlowCase(ExecState*) const;
+ JS_EXPORT_PRIVATE UString toUStringSlowCase(ExecState*) const;
JS_EXPORT_PRIVATE JSObject* toObjectSlowCase(ExecState*, JSGlobalObject*) const;
JS_EXPORT_PRIVATE JSObject* toThisObjectSlowCase(ExecState*) const;
@@ -389,6 +391,9 @@ namespace JSC {
};
#endif
+ // Inline conversion to UString for use only in tight loop.
+ UString fastJSValuetoUString(const JSValue&, ExecState*);
+
typedef HashMap<EncodedJSValue, unsigned, EncodedJSValueHash, EncodedJSValueHashTraits> JSValueMap;
// Stand-alone helper functions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment