Browse Source

Directly add additional properties to Sync instead of overwriting __proto__.

Edward Lee 15 years ago
parent
commit
fe9be233b5
2 changed files with 27 additions and 19 deletions
  1. 18 19
      Sync.js
  2. 9 0
      test/unit/test_Sync.js

+ 18 - 19
Sync.js

@@ -138,23 +138,22 @@ let Sync = function Sync(Function) {
 // Make functions in this module be sync-able
 Sync(Function);
 
-// Change Sync into an Object from a Function, so it loses things like apply and
-// call, but it'll gain all the named properties below.
-Sync.__proto__ = {
-  /**
-   * Sleep the specified number of milliseconds, pausing execution of the caller
-   * without halting the current thread.
-   * For example, the following code pauses 1000ms between dumps:
-   *
-   *   dump("Wait for it...\n");
-   *   Sync.sleep(1000);
-   *   dump("Wait for it...\n");
-   *   Sync.sleep(1000);
-   *   dump("What are you waiting for?!\n");
-   *
-   * @param milliseconds {Number} the number of milliseconds to sleep
-   */
-  sleep: function Sync_sleep(milliseconds) {
-    sleep.sync(milliseconds);
-  }
+// Add additional properties to export with the Sync function/object
+
+/**
+ * Sleep the specified number of milliseconds, pausing execution of the caller
+ * without halting the current thread.
+ * For example, the following code pauses 1000ms between dumps:
+ *
+ *   dump("Wait for it...\n");
+ *   Sync.sleep(1000);
+ *   dump("Wait for it...\n");
+ *   Sync.sleep(1000);
+ *   dump("What are you waiting for?!\n");
+ *
+ * @param milliseconds {Number}
+ *        The number of milliseconds to sleep
+ */
+Sync.sleep = function Sync_sleep(milliseconds) {
+  sleep.sync(milliseconds);
 };

+ 9 - 0
test/unit/test_Sync.js

@@ -22,3 +22,12 @@ function test_Function_prototype_sync() {
   let endTime = new Date();
   do_check_true(endTime - startTime >= 100);
 }
+
+// Make sure the exported Sync object/function has Function properties
+function test_function_Sync() {
+  // We can't check the functions directly because the Function object for Sync
+  // and this test script are compiled against different globals. So do the next
+  // best thing and check if they look the same: function call() [native code]
+  do_check_eq(Sync.call.toSource(), Function.prototype.call.toSource());
+  do_check_eq(Sync.apply.toSource(), Function.prototype.apply.toSource());
+}