Commit c1e6534c authored by tsepez@chromium.org's avatar tsepez@chromium.org

Add support for Float/Double types for JS Mojo bindings.

This is now straightforward using the underlying DataView representation
used by the Mojo JS bindings, given its support for these types.

Review URL: https://codereview.chromium.org/247013008

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@266040 0039d316-1c4b-4281-b951-d872f2087c98
parent 7985c12b
......@@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <limits>
#include "base/command_line.h"
#include "base/file_util.h"
#include "base/files/file_path.h"
......@@ -47,6 +49,17 @@ const uint16 kExpectedUInt16Value = 16961;
const uint32 kExpectedUInt32Value = 1145258561;
const uint64 kExpectedUInt64Value = 77263311946305LL;
// Double/float values, including special case constants.
const double kExpectedDoubleVal = 3.14159265358979323846;
const double kExpectedDoubleInf = std::numeric_limits<double>::infinity();
const double kExpectedDoubleNan = std::numeric_limits<double>::quiet_NaN();
const float kExpectedFloatVal = static_cast<float>(kExpectedDoubleVal);
const float kExpectedFloatInf = std::numeric_limits<float>::infinity();
const float kExpectedFloatNan = std::numeric_limits<float>::quiet_NaN();
// NaN has the property that it is not equal to itself.
#define EXPECT_NAN(x) EXPECT_NE(x, x)
// Returns the path to the mojom js bindings file.
base::FilePath GetFilePathForJSResource(const std::string& path) {
std::string binding_path = "gen/" + path + ".js";
......@@ -144,6 +157,12 @@ class EchoBrowserTargetImpl : public BrowserTargetImpl {
builder.set_ui32(kExpectedUInt32Value);
builder.set_ui16(kExpectedUInt16Value);
builder.set_ui8(kExpectedUInt8Value);
builder.set_float_val(kExpectedFloatVal);
builder.set_float_inf(kExpectedFloatInf);
builder.set_float_nan(kExpectedFloatNan);
builder.set_double_val(kExpectedDoubleVal);
builder.set_double_inf(kExpectedDoubleInf);
builder.set_double_nan(kExpectedDoubleNan);
builder.set_name("coming");
client_->Echo(builder.Finish());
}
......@@ -162,6 +181,12 @@ class EchoBrowserTargetImpl : public BrowserTargetImpl {
EXPECT_EQ(kExpectedUInt32Value, arg1.ui32());
EXPECT_EQ(kExpectedUInt16Value, arg1.ui16());
EXPECT_EQ(kExpectedUInt8Value, arg1.ui8());
EXPECT_EQ(kExpectedFloatVal, arg1.float_val());
EXPECT_EQ(kExpectedFloatInf, arg1.float_inf());
EXPECT_NAN(arg1.float_nan());
EXPECT_EQ(kExpectedDoubleVal, arg1.double_val());
EXPECT_EQ(kExpectedDoubleInf, arg1.double_inf());
EXPECT_NAN(arg1.double_nan());
EXPECT_EQ(std::string("coming"), arg1.name().To<std::string>());
EXPECT_EQ(-1, arg2.si64());
......
module mojo {
// This struct encompasses all of the basic types, so that they
// may be sent from C++ to JS and back for validation.
struct EchoArgs {
int64 si64;
int32 si32;
......@@ -9,6 +11,12 @@ struct EchoArgs {
uint32 ui32;
uint16 ui16;
uint8 ui8;
float float_val;
float float_inf;
float float_nan;
double double_val;
double double_inf;
double double_nan;
string name;
};
......
......@@ -178,15 +178,15 @@ define("mojo/public/js/bindings/codec", [
return result;
};
Decoder.prototype.readFloat32 = function() {
var result = this.buffer.dataView.readFloat32(
Decoder.prototype.readFloat = function() {
var result = this.buffer.dataView.getFloat32(
this.next, kHostIsLittleEndian);
this.next += 4;
return result;
};
Decoder.prototype.readFloat64 = function() {
var result = this.buffer.dataView.readFloat64(
Decoder.prototype.readDouble = function() {
var result = this.buffer.dataView.getFloat64(
this.next, kHostIsLittleEndian);
this.next += 8;
return result;
......@@ -308,13 +308,13 @@ define("mojo/public/js/bindings/codec", [
this.next += 8;
};
Encoder.prototype.writeFloat32 = function(val) {
this.buffer.dataView.setFloat32(val, kHostIsLittleEndian);
Encoder.prototype.writeFloat = function(val) {
this.buffer.dataView.setFloat32(this.next, val, kHostIsLittleEndian);
this.next += 4;
};
Encoder.prototype.writeFloat64 = function(val) {
this.buffer.dataView.setFloat64(val, kHostIsLittleEndian);
Encoder.prototype.writeDouble = function(val) {
this.buffer.dataView.setFloat64(this.next, val, kHostIsLittleEndian);
this.next += 8;
};
......@@ -558,7 +558,7 @@ define("mojo/public/js/bindings/codec", [
};
function Int64() {
};
}
Int64.encodedSize = 8;
......@@ -571,7 +571,7 @@ define("mojo/public/js/bindings/codec", [
};
function Uint64() {
};
}
Uint64.encodedSize = 8;
......@@ -583,15 +583,38 @@ define("mojo/public/js/bindings/codec", [
encoder.writeUint64(val);
};
function PointerTo(cls) {
this.cls = cls;
};
// TODO(abarth): Add missing types:
// * String
// * Float
// * Double
// * Signed integers
function Float() {
}
Float.encodedSize = 4;
Float.decode = function(decoder) {
return decoder.readFloat();
};
Float.encode = function(encoder, val) {
encoder.writeFloat(val);
};
function Double() {
}
Double.encodedSize = 8;
Double.decode = function(decoder) {
return decoder.readDouble();
};
Double.encode = function(encoder, val) {
encoder.writeDouble(val);
};
function PointerTo(cls) {
this.cls = cls;
}
PointerTo.prototype.encodedSize = 8;
......@@ -606,7 +629,7 @@ define("mojo/public/js/bindings/codec", [
function ArrayOf(cls) {
this.cls = cls;
};
}
ArrayOf.prototype.encodedSize = 8;
......@@ -651,6 +674,8 @@ define("mojo/public/js/bindings/codec", [
exports.Uint32 = Uint32;
exports.Int64 = Int64;
exports.Uint64 = Uint64;
exports.Float = Float;
exports.Double = Double;
exports.PointerTo = PointerTo;
exports.ArrayOf = ArrayOf;
exports.Handle = Handle;
......
......@@ -98,7 +98,7 @@ _kind_to_javascript_decode_snippet = {
mojom.UINT16: "readUint16()",
mojom.INT32: "readInt32()",
mojom.UINT32: "readUint32()",
mojom.FLOAT: "decodeFloat()",
mojom.FLOAT: "readFloat()",
mojom.HANDLE: "decodeHandle()",
mojom.DCPIPE: "decodeHandle()",
mojom.DPPIPE: "decodeHandle()",
......@@ -106,7 +106,7 @@ _kind_to_javascript_decode_snippet = {
mojom.SHAREDBUFFER: "decodeHandle()",
mojom.INT64: "readInt64()",
mojom.UINT64: "readUint64()",
mojom.DOUBLE: "decodeDouble()",
mojom.DOUBLE: "readDouble()",
mojom.STRING: "decodeStringPointer()",
}
......@@ -132,7 +132,7 @@ _kind_to_javascript_encode_snippet = {
mojom.UINT16: "writeUint16(",
mojom.INT32: "writeInt32(",
mojom.UINT32: "writeUint32(",
mojom.FLOAT: "encodeFloat(",
mojom.FLOAT: "writeFloat(",
mojom.HANDLE: "encodeHandle(",
mojom.DCPIPE: "encodeHandle(",
mojom.DPPIPE: "encodeHandle(",
......@@ -140,7 +140,7 @@ _kind_to_javascript_encode_snippet = {
mojom.SHAREDBUFFER: "encodeHandle(",
mojom.INT64: "writeInt64(",
mojom.UINT64: "writeUint64(",
mojom.DOUBLE: "encodeDouble(",
mojom.DOUBLE: "writeDouble(",
mojom.STRING: "encodeStringPointer(",
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment