Hello michaeln,

I'd like you to do a code review.  Please execute
        g4 diff -c 10487690

or point your web browser to
        http://mondrian/10487690

to review the following code:

Change 10487690 by nigel...@nigeltao-srcgears5 on 2009/03/16 15:39:25 *pending*

        Introduce blob.getByte as a JS-visible method.
        
        PRESUBMIT=passed
        R=michaeln
        [email protected]
        DELTA=48  (47 added, 0 deleted, 1 changed)
        OCL=10487690

Affected files ...

... //depot/googleclient/gears/opensource/gears/blob/blob.cc#12 edit
... //depot/googleclient/gears/opensource/gears/blob/blob.h#5 edit
... //depot/googleclient/gears/opensource/gears/test/testcases/blob_tests.js#2 
edit

48 delta lines: 47 added, 0 deleted, 1 changed

Also consider running:
        g4 lint -c 10487690

which verifies that the changelist doesn't introduce new style violations.

If you can't do the review, please let me know as soon as possible.  During
your review, please ensure that all new code has corresponding unit tests and
that existing unit tests are updated appropriately.  Visit
http://www/eng/code_review.html for more information.

This is a semiautomated message from "g4 mail".  Complaints or suggestions?
Mail [email protected].
Change 10487690 by nigel...@nigeltao-srcgears5 on 2009/03/16 15:39:25 *pending*

        Introduce blob.getByte as a JS-visible method.

Affected files ...

... //depot/googleclient/gears/opensource/gears/blob/blob.cc#12 edit
... //depot/googleclient/gears/opensource/gears/blob/blob.h#5 edit
... //depot/googleclient/gears/opensource/gears/test/testcases/blob_tests.js#2 
edit

==== //depot/googleclient/gears/opensource/gears/blob/blob.cc#12 - 
/home/nigeltao/srcgears5/googleclient/gears/opensource/gears/blob/blob.cc ====
# action=edit type=text
--- googleclient/gears/opensource/gears/blob/blob.cc    2009-03-16 
15:39:38.000000000 +1100
+++ googleclient/gears/opensource/gears/blob/blob.cc    2009-03-16 
15:31:43.000000000 +1100
@@ -34,6 +34,7 @@
 #ifdef DEBUG
   RegisterMethod("hasSameContentsAs", &GearsBlob::HasSameContentsAs);
 #endif
+  RegisterMethod("getByte", &GearsBlob::GetByte);
   RegisterMethod("slice", &GearsBlob::Slice);
   RegisterProperty("length", &GearsBlob::GetLength, NULL);
 }
@@ -86,6 +87,29 @@
   context->SetReturnValue(JSPARAM_BOOL, &result);
 }
 #endif
+
+void GearsBlob::GetByte(JsCallContext *context) {
+  int64 index = 0;
+  JsArgument argv[] = {
+    { JSPARAM_REQUIRED, JSPARAM_INT64, &index },
+  };
+  if (!context->GetArguments(ARRAYSIZE(argv), argv)) {
+    assert(context->is_exception_set());
+    return;
+  }
+
+  if (index < 0 || index >= contents_->Length()) {
+    context->SetException(STRING16(L"Index to getByte is out of bounds."));
+    return;
+  }
+  uint8 result_as_byte;
+  if ((contents_->Read(&result_as_byte, index, 1) != 1)) {
+    context->SetException(GET_INTERNAL_ERROR_MESSAGE());
+    return;
+  }
+  int result = static_cast<int>(result_as_byte);
+  context->SetReturnValue(JSPARAM_INT, &result);
+}
 
 void GearsBlob::GetLength(JsCallContext *context) {
   // A GearsBlob should never be let out in the JS world unless it has been
==== //depot/googleclient/gears/opensource/gears/blob/blob.h#5 - 
/home/nigeltao/srcgears5/googleclient/gears/opensource/gears/blob/blob.h ====
# action=edit type=text
--- googleclient/gears/opensource/gears/blob/blob.h     2009-03-16 
15:39:38.000000000 +1100
+++ googleclient/gears/opensource/gears/blob/blob.h     2009-03-16 
14:52:35.000000000 +1100
@@ -39,6 +39,10 @@
       : ModuleImplBaseClass(kModuleName),
         contents_(new EmptyBlob()) {}
 
+  // IN: int64 index
+  // OUT: int
+  void GetByte(JsCallContext *context);
+
   // IN: nothing
   // OUT: int64
   void GetLength(JsCallContext *context);
==== //depot/googleclient/gears/opensource/gears/test/testcases/blob_tests.js#2 
- 
/home/nigeltao/srcgears5/googleclient/gears/opensource/gears/test/testcases/blob_tests.js
 ====
# action=edit type=text
--- googleclient/gears/opensource/gears/test/testcases/blob_tests.js    
2009-03-16 15:39:39.000000000 +1100
+++ googleclient/gears/opensource/gears/test/testcases/blob_tests.js    
2009-03-16 15:31:45.000000000 +1100
@@ -69,5 +69,24 @@
   var builder6 = google.gears.factory.create('beta.blobbuilder');
   builder6.append('\uCF8F');  // 3 bytes in utf8
   builder6.append('\u00A2');  // 2 bytes in utf8
-  assert(builder6.getAsBlob().length, 3 + 2);
+  var blob6 = builder6.getAsBlob();
+  assert(blob6.length, 3 + 2);
+
+  // \uCF8F is 1100 1111 1000 1111 in UTF-16, which in UTF-8 is
+  // iiio 1100 io11 1110 io00 1111, where 'i' and 'o' are ones and zeroes
+  // introduced by the UTF-8 encoding, and '1' and '0' are the content.
+  // Converting these three bytes to hex gives EC BE 8F, or 236 190 143.
+  // Similarly, \u00A2 becomes iio0 0010 io10 0010, or C2 A2, or 194 162.
+  assertEqual(236, blob6.getByte(0));
+  assertEqual(190, blob6.getByte(1));
+  assertEqual(143, blob6.getByte(2));
+  assertEqual(194, blob6.getByte(3));
+  assertEqual(162, blob6.getByte(4));
+
+  assertError(function() {
+    blob6.getByte(-1);
+  }, 'Index to getByte is out of bounds.');
+  assertError(function() {
+    blob6.getByte(5);
+  }, 'Index to getByte is out of bounds.');
 }

Reply via email to