Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(382)

Unified Diff: Source/bindings/v8/V8Binding.cpp

Issue 16951005: Add support for byte and octet Web IDL types to the bindings generator (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Take Kentaro's feedback into consideration Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: Source/bindings/v8/V8Binding.cpp
diff --git a/Source/bindings/v8/V8Binding.cpp b/Source/bindings/v8/V8Binding.cpp
index 9bd2b00b53eb63f053611e22034683d387649fd4..94400faf1aab3068d62a68b2121763275b1817b5 100644
--- a/Source/bindings/v8/V8Binding.cpp
+++ b/Source/bindings/v8/V8Binding.cpp
@@ -115,6 +115,9 @@ PassRefPtr<NodeFilter> toNodeFilter(v8::Handle<v8::Value> callback)
return NodeFilter::create(V8NodeFilterCondition::create(callback));
}
+static const int8_t kMaxInt8 = 127;
+static const int8_t kMinInt8 = -128;
+static const uint8_t kMaxUInt8 = 255;
const int32_t kMaxInt32 = 0x7fffffff;
const int32_t kMinInt32 = -kMaxInt32 - 1;
const uint32_t kMaxUInt32 = 0xffffffff;
@@ -134,6 +137,78 @@ static double enforceRange(double x, double minimum, double maximum, bool& ok)
return x;
}
+int8_t toInt8(v8::Handle<v8::Value> value, IntegerConversionConfiguration configuration, bool& ok)
+{
+ ok = true;
+
+ // Fast case. The value is already a 32-bit integer in the right range.
+ if (value->IsInt32()) {
+ int32_t result = value->Int32Value();
+ if (result >= kMinInt8 && result <= kMaxInt8)
+ return static_cast<int8_t>(result);
+ if (configuration == EnforceRange) {
+ ok = false;
+ return 0;
+ }
+ result %= 256; // 2^8.
+ return static_cast<int8_t>(result > kMaxInt8 ? result - 256 : result);
jsbell 2013/06/13 16:51:31 These two lines can be simplified to just: return
do-not-use 2013/06/13 19:11:20 AFAIK, modulus behavior is only guaranteed for uns
jsbell 2013/06/13 19:14:23 Right, sorry. Carry on!
+ }
+
+ // Can the value be converted to a number?
+ v8::Local<v8::Number> numberObject = value->ToNumber();
+ if (numberObject.IsEmpty()) {
+ ok = false;
+ return 0;
+ }
+
+ if (configuration == EnforceRange)
+ return enforceRange(numberObject->Value(), kMinInt8, kMaxInt8, ok);
+
+ double numberValue = numberObject->Value();
+ if (std::isnan(numberValue) || std::isinf(numberValue) || !numberValue)
+ return 0;
+
+ numberValue = numberValue < 0 ? -floor(abs(numberValue)) : floor(abs(numberValue));
+ numberValue = fmod(numberValue, 256); // 2^8.
+
+ return static_cast<int8_t>(numberValue > kMaxInt8 ? numberValue - 256 : numberValue);
jsbell 2013/06/13 16:51:31 Alternately, this line could be: return static_cas
+}
+
+uint8_t toUInt8(v8::Handle<v8::Value> value, IntegerConversionConfiguration configuration, bool& ok)
+{
+ ok = true;
+
+ // Fast case. The value is a 32-bit signed integer - possibly positive?
+ if (value->IsInt32()) {
+ int32_t result = value->Int32Value();
+ if (result >= 0 && result <= kMaxUInt8)
+ return static_cast<uint8_t>(result);
+ if (configuration == EnforceRange) {
+ ok = false;
+ return 0;
+ }
+ return static_cast<uint8_t>(result % 256); // 2^8.
jsbell 2013/06/13 16:51:31 The modulus should not be necessary here.
do-not-use 2013/06/13 19:11:20 Yes, here it is not technically required since the
+ }
+
+ // Can the value be converted to a number?
+ v8::Local<v8::Number> numberObject = value->ToNumber();
+ if (numberObject.IsEmpty()) {
+ ok = false;
+ return 0;
+ }
+
+ if (configuration == EnforceRange)
+ return enforceRange(numberObject->Value(), 0, kMaxUInt8, ok);
+
+ // Does the value convert to nan or to an infinity?
+ double numberValue = numberObject->Value();
+ if (std::isnan(numberValue) || std::isinf(numberValue) || !numberValue)
+ return 0;
+
+ numberValue = numberValue < 0 ? -floor(abs(numberValue)) : floor(abs(numberValue));
+ return static_cast<uint8_t>(fmod(numberValue, 256)); // 2^8.
+}
+
int32_t toInt32(v8::Handle<v8::Value> value, IntegerConversionConfiguration configuration, bool& ok)
{
ok = true;

Powered by Google App Engine
This is Rietveld 408576698