http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/abd646b2/proton-c/bindings/javascript/data-array.js ---------------------------------------------------------------------- diff --git a/proton-c/bindings/javascript/data-array.js b/proton-c/bindings/javascript/data-array.js new file mode 100644 index 0000000..6f9776d --- /dev/null +++ b/proton-c/bindings/javascript/data-array.js @@ -0,0 +1,100 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +/*****************************************************************************/ +/* */ +/* proton.Data.Array */ +/* */ +/*****************************************************************************/ + +/** + * TODO make this behave more like a native JavaScript Array: http://www.bennadel.com/blog/2292-extending-javascript-arrays-while-keeping-native-bracket-notation-functionality.htm + * Create a proton.Data.Array. + * @classdesc + * This class represents an AMQP Array. + * @constructor proton.Data.Array + * @param {string|number} type the type of the Number either as a string or number. + * Stored internally as a string corresponding to one of the TypeNames. + * @param {Array|TypedArray} elements the Native JavaScript Array or TypedArray that we wish to serialise. + * @param {object} descriptor an optional object describing the type. + */ +Data['Array'] = function(type, elements, descriptor) { // Data.Array Constructor. + // This block caters for an empty Array or a Described empty Array. + if (arguments.length < 2) { + descriptor = type; + type = 'NULL'; + elements = []; + } + + this['type'] = (typeof type === 'number') ? Data['TypeNames'][type] : type; + this['elements'] = elements; + this['descriptor'] = descriptor; +}; + +/** + * @method toString + * @memberof! proton.Data.Array# + * @returns {string} the String form of a {@link proton.Data.Array}. + */ +Data['Array'].prototype.toString = function() { + var descriptor = (this['descriptor'] == null) ? '' : ':' + this['descriptor']; + return this['type'] + 'Array' + descriptor + '[' + this['elements'] + ']'; +}; + +/** + * @method valueOf + * @memberof! proton.Data.Array# + * @returns {Array} the elements of the {@link proton.Data.Array}. + */ +Data['Array'].prototype.valueOf = function() { + return this['elements']; +}; + +/** + * Compare two instances of proton.Data.Array for equality. N.B. this method + * compares the value of every Array element so its performance is O(n). + * @method equals + * @memberof! proton.Data.Array# + * @param {proton.Data.Array} rhs the instance we wish to compare this instance with. + * @returns {boolean} true iff the two compared instances are equal. + */ +Data['Array'].prototype['equals'] = function(rhs) { + if (rhs instanceof Data['Array'] && + // Check the string value of the descriptors. + (('' + this['descriptor']) === ('' + rhs['descriptor'])) && + (this['type'] === rhs['type'])) { + var elements = this['elements']; + var relements = rhs['elements']; + var length = elements.length; + if (length === relements.length) { + for (var i = 0; i < length; i++) { + if (elements[i].valueOf() !== relements[i].valueOf()) { + return false; + } + } + return true; + } else { + return false; + } + } else { + return false; + } +}; +
http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/abd646b2/proton-c/bindings/javascript/data-binary.js ---------------------------------------------------------------------- diff --git a/proton-c/bindings/javascript/data-binary.js b/proton-c/bindings/javascript/data-binary.js new file mode 100644 index 0000000..27bac59 --- /dev/null +++ b/proton-c/bindings/javascript/data-binary.js @@ -0,0 +1,181 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +/*****************************************************************************/ +/* */ +/* proton.Data.Binary */ +/* */ +/*****************************************************************************/ + +/** + * Create a proton.Data.Binary. This constructor takes one or two parameters. + * The first parameter may serve different purposes depending on its type; + * If value is a number then it represents the size of the Binary data buffer, + * if it is a string then we copy the string to the buffer, if it is an Array + * or a TypedArray then we copy the data to the buffer. The optional second + * parameter is a pointer to the data in an internal data store. If start is + * not specified then the number of bytes specified in the first parameter + * will be allocated in the internal data store and start will point to the + * start of that block of data. + * @classdesc + * This class represents an AMQP Binary type. This class allows us to create and + * use raw binary data and map it efficiently between JavaScript client code and + * the underlying implementation, where all data is managed on a "virtual heap". + * <p> + * Client applications should generally not have to care about memory management + * as, for most common use cases, client applications would "transfer ownership" + * to a "container" which would then "own" the underlying data and free the data + * held by the {@link proton.Data.Binary}. + * <p> + * As an example one common use-case would be where client application creates a + * {@link proton.Data.Binary} specifying the required size. It would usually then + * call getBuffer() to access the underlying Uint8Array. At this point the client + * "owns" the data and so would have to call free() if it did nothing more with + * the Binary, however when {@link proton.Data.putBINARY} is called the ownership + * of the raw data on the virtual heap transfers from the Binary to the Data and + * the client no longer needs to call free() itself. In this case the putBINARY() + * call transfers ownership and can then safely call free() on the Binary. + * <p> + * Conversely a common use-case when receiving data is where a Binary may be + * created by {@link proton.Data#getBINARY}. In this case the Binary is simply a + * "view" onto the bytes owned by the Data instance. A client application can + * safely access the bytes from the view, but if it wishes to use the bytes beyond + * the scope of the Data instance (e.g. after the next {@link proton.Messenger#get} + * call then the client must explicitly *copy* the bytes into a new buffer, for + * example via copyBuffer(). + * <p> + * Most of the {@link proton.Data} methods that take {@link proton.Data.Binary} + * as a parameter "consume" the underlying data and take responsibility for + * freeing it from the heap e.g. {@link proton.Data#putBINARY}, {@link proton.Data#decode}. + * For the methods that return a {@link proton.Data.Binary} the call to + * {@link proton.Data#getBINARY}, which is the most common case, returns a Binary + * that has a "view" of the underlying data that is actually owned by the Data + * instance and thus doesn't need to be explicitly freed on the Binary. The call + * to {@link proton.Data#encode} however returns a Binary that represents a *copy* + * of the underlying data, in this case (like a client calling new proton.Data.Binary) + * the client takes responsibility for freeing the data, unless of course it is + * subsequently passed to a method that will consume the data (putBINARY/decode). + * @constructor proton.Data.Binary + * @param {(number|string|Array|TypedArray)} value If value is a number then it + * represents the size of the Binary data buffer, if it is a string then + * we copy the string to the buffer, if it is an Array or a TypedArray + * then we copy the data to the buffer. N.B. although convenient do bear + * in mind that using a mechanism other than constructing with a simple + * size will result in some form of additional data copy. + * @param {number} start an optional pointer to the start of the Binary data buffer. + */ +Data['Binary'] = function(value, start) { // Data.Binary Constructor. + /** + * If the start pointer is specified then the underlying binary data is owned + * by another object, so we set the call to free to be a null function. If + * the start pointer is not passed then we allocate storage of the specified + * size on the emscripten heap and set the call to free to free the data from + * the emscripten heap. + */ + var size = value; + if (start) { + this['free'] = function() {}; + } else { // Create Binary from Array, ArrayBuffer or TypedArray. + var hasArrayBuffer = (typeof ArrayBuffer === 'function'); + if (Data.isArray(value) || + (hasArrayBuffer && value instanceof ArrayBuffer) || + (value.buffer && hasArrayBuffer && value.buffer instanceof ArrayBuffer)) { + value = new Uint8Array(value); + size = value.length; + start = _malloc(size); // Allocate storage from emscripten heap. + Module['HEAPU8'].set(value, start); // Copy the data to the emscripten heap. + } else if (Data.isString(value)) { // Create Binary from native string + value = unescape(encodeURIComponent(value)); // Create a C-like UTF representation. + size = value.length; + start = _malloc(size); // Allocate storage from emscripten heap. + for (var i = 0; i < size; i++) { + setValue(start + i, value.charCodeAt(i), 'i8', 1); + } + } else { // Create unpopulated Binary of specified size. + // If the type is not a number by this point then an unrecognised data + // type has been passed so we create a zero length Binary. + size = Data.isNumber(size) ? size : 0; + start = _malloc(size); // Allocate storage from emscripten heap. + } + this['free'] = function() { + _free(this.start); + this.size = 0; + this.start = 0; + // Set free to a null function to prevent possibility of a "double free". + this['free'] = function() {}; + }; + } + + this.size = size; + this.start = start; +}; + +/** + * Get a Uint8Array view of the data. N.B. this is just a *view* of the data, + * which will go out of scope on the next call to {@link proton.Messenger.get}. If + * a client wants to retain the data then copy should be used to explicitly + * create a copy of the data which the client then owns to do with as it wishes. + * @method getBuffer + * @returns {Uint8Array} a new Uint8Array view of the data. + * @memberof! proton.Data.Binary# + */ +Data['Binary'].prototype['getBuffer'] = function() { + return new Uint8Array(HEAPU8.buffer, this.start, this.size); +}; + +/** + * Explicitly create a *copy* of the Binary, copying the underlying binary data too. + * @method copy + * @param {number} offset an optional offset into the underlying buffer from + * where we want to copy the data, default is zero. + * @param {number} n an optional number of bytes to copy, default is this.size - offset. + * @returns {proton.Data.Binary} a new {@link proton.Data.Binary} created by copying the underlying binary data. + * @memberof! proton.Data.Binary# + */ +Data['Binary'].prototype['copy'] = function(offset, n) { + offset = offset | 0; + n = n ? n : (this.size - offset); + + if (offset >= this.size) { + offset = 0; + n = 0; + } else if ((offset + n) > this.size) { + n = this.size - offset; // Clamp length + } + + var start = _malloc(n); // Allocate storage from emscripten heap. + _memcpy(start, this.start + offset, n); // Copy the raw data to new buffer. + + return new Data['Binary'](n, start); +}; + +/** + * Converts the {@link proton.Data.Binary} to a string. This is clearly most + * useful when the binary data is actually a binary representation of a string + * such as a C style ASCII string. + * @method toString + * @memberof! proton.Data.Binary# + * @returns {string} the String form of a {@link proton.Data.Binary}. + */ +Data['Binary'].prototype.toString = Data['Binary'].prototype.valueOf = function() { + // Create a native JavaScript String from the start/size information. + return Pointer_stringify(this.start, this.size); +}; + http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/abd646b2/proton-c/bindings/javascript/data-described.js ---------------------------------------------------------------------- diff --git a/proton-c/bindings/javascript/data-described.js b/proton-c/bindings/javascript/data-described.js new file mode 100644 index 0000000..e1f9d84 --- /dev/null +++ b/proton-c/bindings/javascript/data-described.js @@ -0,0 +1,74 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +/*****************************************************************************/ +/* */ +/* proton.Data.Described */ +/* */ +/*****************************************************************************/ + +/** + * Create a proton.Data.Described. + * @classdesc + * This class represents an AMQP Described. + * @constructor proton.Data.Described + * @param {object} value the value of the described type. + * @param {string} descriptor an optional string describing the type. + * @property {object} value the actual value of the described type. + * @property {string} descriptor a string describing the type. + */ +Data['Described'] = function(value, descriptor) { // Data.Described Constructor. + this['value'] = value; + this['descriptor'] = descriptor; +}; + +/** + * @method toString + * @memberof! proton.Data.Described# + * @returns {string} the String form of a {@link proton.Data.Described}. + */ +Data['Described'].prototype.toString = function() { + return 'Described(' + this['value'] + ', ' + this['descriptor'] + ')'; +}; + +/** + * @method valueOf + * @memberof! proton.Data.Described# + * @returns {object} the value of the {@link proton.Data.Described}. + */ +Data['Described'].prototype.valueOf = function() { + return this['value']; +}; + +/** + * Compare two instances of proton.Data.Described for equality. + * @method equals + * @memberof! proton.Data.Described# + * @param {proton.Data.Described} rhs the instance we wish to compare this instance with. + * @returns {boolean} true iff the two compared instances are equal. + */ +Data['Described'].prototype['equals'] = function(rhs) { + if (rhs instanceof Data['Described']) { + return ((this['descriptor'] === rhs['descriptor']) && (this['value'] === rhs['value'])); + } else { + return false; + } +}; + http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/abd646b2/proton-c/bindings/javascript/data-long.js ---------------------------------------------------------------------- diff --git a/proton-c/bindings/javascript/data-long.js b/proton-c/bindings/javascript/data-long.js new file mode 100644 index 0000000..38fb6c8 --- /dev/null +++ b/proton-c/bindings/javascript/data-long.js @@ -0,0 +1,191 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +/*****************************************************************************/ +/* */ +/* proton.Data.Long */ +/* */ +/*****************************************************************************/ + +/** + * Create a proton.Data.Long. + * @classdesc + * This class represents a 64 bit Integer value. It is used primarily to pass and + * return 64 bit Integer values to and from the emscripten compiled proton-c library. + * This class is needed because JavaScript cannot natively represent 64 bit + * Integers with sufficient accuracy. + * @constructor proton.Data.Long + * @param {number} low the least significant word. + * @param {number} high the most significant word. + */ +// Use dot notation as it is a "protected" inner class not exported from the closure. +Data.Long = function(low, high) { // Data.Long Constructor. + this.low = low | 0; // force into 32 signed bits. + this.high = high | 0; // force into 32 signed bits. +}; + +// proton.Data.Long constants. +Data.Long.TWO_PWR_16_DBL_ = 1 << 16; +Data.Long.TWO_PWR_32_DBL_ = Data.Long.TWO_PWR_16_DBL_ * Data.Long.TWO_PWR_16_DBL_; +Data.Long.TWO_PWR_64_DBL_ = Data.Long.TWO_PWR_32_DBL_ * Data.Long.TWO_PWR_32_DBL_; +Data.Long.TWO_PWR_63_DBL_ = Data.Long.TWO_PWR_64_DBL_ / 2; +Data.Long.MAX_VALUE = new Data.Long(0xFFFFFFFF | 0, 0x7FFFFFFF | 0); +Data.Long.MIN_VALUE = new Data.Long(0, 0x80000000 | 0); +Data.Long.ZERO = new Data.Long(0, 0); +Data.Long.ONE = new Data.Long(1, 0); + +/** + * @method fromNumber + * @memberof! proton.Data.Long# + * @returns {proton.Data.Long} an instance of {@link proton.Data.Long} created + * using a native JavaScript number. + */ +Data.Long.fromNumber = function(value) { + if (isNaN(value) || !isFinite(value)) { + return Data.Long.ZERO; + } else if (value <= -Data.Long.TWO_PWR_63_DBL_) { + return Data.Long.MIN_VALUE; + } else if (value + 1 >= Data.Long.TWO_PWR_63_DBL_) { + return Data.Long.MAX_VALUE; + } else if (value < 0) { + return Data.Long.fromNumber(-value).negate(); + } else { + return new Data.Long( + (value % Data.Long.TWO_PWR_32_DBL_) | 0, + (value / Data.Long.TWO_PWR_32_DBL_) | 0); + } +}; + +/** + * Return the twos complement of this instance. + * @method negate + * @memberof! proton.Data.Long# + * @returns {proton.Data.Long} the twos complement of this instance. + */ +Data.Long.prototype.negate = function() { + if (this.equals(Data.Long.MIN_VALUE)) { + return Data.Long.MIN_VALUE; + } else { + return this.not().add(Data.Long.ONE); + } +}; + +/** + * Add two instances of {@link proton.Data.Long}. + * @method add + * @memberof! proton.Data.Long# + * @param {proton.Data.Long} rhs the instance we wish to add to this instance. + * @returns {proton.Data.Long} the sum of this value and the rhs. + */ +Data.Long.prototype.add = function(rhs) { + // Divide each number into 4 chunks of 16 bits, and then sum the chunks. + + var a48 = this.high >>> 16; + var a32 = this.high & 0xFFFF; + var a16 = this.low >>> 16; + var a00 = this.low & 0xFFFF; + + var b48 = rhs.high >>> 16; + var b32 = rhs.high & 0xFFFF; + var b16 = rhs.low >>> 16; + var b00 = rhs.low & 0xFFFF; + + var c48 = 0, c32 = 0, c16 = 0, c00 = 0; + c00 += a00 + b00; + c16 += c00 >>> 16; + c00 &= 0xFFFF; + c16 += a16 + b16; + c32 += c16 >>> 16; + c16 &= 0xFFFF; + c32 += a32 + b32; + c48 += c32 >>> 16; + c32 &= 0xFFFF; + c48 += a48 + b48; + c48 &= 0xFFFF; + return new Data.Long((c16 << 16) | c00, (c48 << 16) | c32); +}; + +/** + * Return the complement of this instance. + * @method not + * @memberof! proton.Data.Long# + * @returns {proton.Data.Long} the complement of this instance. + */ +Data.Long.prototype.not = function() { + return new Data.Long(~this.low, ~this.high); +}; + +/** + * Compare two instances of {@link proton.Data.Long} for equality. + * @method equals + * @memberof! proton.Data.Long# + * @param {proton.Data.Long} rhs the instance we wish to compare this instance with. + * @returns {boolean} true iff the two compared instances are equal. + */ +Data.Long.prototype.equals = function(other) { + return (this.high == other.high) && (this.low == other.low); +}; + +/** + * @method getHighBits + * @memberof! proton.Data.Long# + * @returns {number} the most significant word of a {@link proton.Data.Long}. + */ +Data.Long.prototype.getHighBits = function() { + return this.high; +}; + +/** + * @method getLowBits + * @memberof! proton.Data.Long# + * @returns {number} the least significant word of a {@link proton.Data.Long}. + */ +Data.Long.prototype.getLowBits = function() { + return this.low; +}; + +/** + * @method getLowBitsUnsigned + * @memberof! proton.Data.Long# + * @returns {number} the least significant word of a {@link proton.Data.Long} + * as an unsigned value. + */ +Data.Long.prototype.getLowBitsUnsigned = function() { + return (this.low >= 0) ? this.low : Data.Long.TWO_PWR_32_DBL_ + this.low; +}; + +/** + * @method toNumber + * @memberof! proton.Data.Long# + * @returns {number} a native JavaScript number (with possible loss of precision). + */ +Data.Long.prototype.toNumber = function() { + return (this.high * Data.Long.TWO_PWR_32_DBL_) + this.getLowBitsUnsigned(); +}; + +/** + * @method toString + * @memberof! proton.Data.Long# + * @returns {string} the String form of a {@link proton.Data.Long}. + */ +Data.Long.prototype.toString = function() { + return this.high + ':' + this.getLowBitsUnsigned(); +}; + http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/abd646b2/proton-c/bindings/javascript/data-symbol.js ---------------------------------------------------------------------- diff --git a/proton-c/bindings/javascript/data-symbol.js b/proton-c/bindings/javascript/data-symbol.js new file mode 100644 index 0000000..c742acb --- /dev/null +++ b/proton-c/bindings/javascript/data-symbol.js @@ -0,0 +1,59 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +/*****************************************************************************/ +/* */ +/* proton.Data.Symbol */ +/* */ +/*****************************************************************************/ + +/** + * Create a proton.Data.Symbol. + * @classdesc + * This class represents an AMQP Symbol. This class is necessary primarily as a + * way to enable us to distinguish between a native String and a Symbol in the + * JavaScript type system. + * @constructor proton.Data.Symbol + * @param {string} s a symbol. + */ +Data['Symbol'] = function(s) { // Data.Symbol Constructor. + this.value = s; +}; + +/** + * @method toString + * @memberof! proton.Data.Symbol# + * @returns {string} the String form of a {@link proton.Data.Symbol}. + */ +Data['Symbol'].prototype.toString = Data['Symbol'].prototype.valueOf = function() { + return this.value; +}; + +/** + * Compare two instances of proton.Data.Symbol for equality. + * @method equals + * @memberof! proton.Data.Symbol# + * @param {proton.Data.Symbol} rhs the instance we wish to compare this instance with. + * @returns {boolean} true iff the two compared instances are equal. + */ +Data['Symbol'].prototype['equals'] = function(rhs) { + return this.toString() === rhs.toString(); +}; + http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/abd646b2/proton-c/bindings/javascript/data-typed-number.js ---------------------------------------------------------------------- diff --git a/proton-c/bindings/javascript/data-typed-number.js b/proton-c/bindings/javascript/data-typed-number.js new file mode 100644 index 0000000..034d4ec --- /dev/null +++ b/proton-c/bindings/javascript/data-typed-number.js @@ -0,0 +1,108 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +/*****************************************************************************/ +/* */ +/* proton.Data.TypedNumber */ +/* */ +/*****************************************************************************/ + +// ---------------------- JavaScript Number Extensions ------------------------ + +Number.prototype['ubyte'] = function() { + return new Data.TypedNumber('UBYTE', this); +}; + +Number.prototype['byte'] = function() { + return new Data.TypedNumber('BYTE', this); +}; + +Number.prototype['ushort'] = function() { + return new Data.TypedNumber('USHORT', this); +}; + +Number.prototype['short'] = function() { + return new Data.TypedNumber('SHORT', this); +}; + +Number.prototype['uint'] = function() { + return new Data.TypedNumber('UINT', this); +}; + +Number.prototype['int'] = function() { + return new Data.TypedNumber('INT', this); +}; + +Number.prototype['ulong'] = function() { + return new Data.TypedNumber('ULONG', this); +}; + +Number.prototype['long'] = function() { + return new Data.TypedNumber('LONG', this); +}; + +Number.prototype['float'] = function() { + return new Data.TypedNumber('FLOAT', this); +}; + +Number.prototype['double'] = function() { + return new Data.TypedNumber('DOUBLE', this); +}; + +Number.prototype['char'] = function() { + return new Data.TypedNumber('CHAR', this); +}; + +String.prototype['char'] = function() { + return new Data.TypedNumber('CHAR', this.charCodeAt(0)); +}; + +// ------------------------- proton.Data.TypedNumber -------------------------- +/** + * Create a proton.Data.TypedNumber. + * @classdesc + * This class is a simple wrapper class that allows a "type" to be recorded for + * a number. The idea is that the JavaScript Number class is extended with extra + * methods to allow numbers to be "modified" to TypedNumbers, so for example + * 1.0.float() would modify 1.0 by returning a TypedNumber with type = FLOAT + * and value = 1. The strings used for type correspond to the names of the Data + * put* methods e.g. UBYTE, BYTE, USHORT, SHORT, UINT, INT, ULONG, LONG, FLOAT, + * DOUBLE, CHAR so that the correct method to call can be derived from the type. + * @constructor proton.Data.TypedNumber + * @param {(string|number)} type the type of the Number either as a string or number. + * Stored internally as a string corresponding to one of the TypeNames. + * @param {number} value the value of the Number. + */ +// Use dot notation as it is a "protected" inner class not exported from the closure. +Data.TypedNumber = function(type, value) { // Data.TypedNumber Constructor. + this.type = (typeof type === 'number') ? Data['TypeNames'][type] : type; + this.value = value; +}; + +/** + * @method toString + * @memberof! proton.Data.TypedNumber# + * @returns {string} the String form of a {@link proton.Data.TypedNumber}. + */ +Data.TypedNumber.prototype.toString = Data.TypedNumber.prototype.valueOf = function() { + return +this.value; +}; + + http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/abd646b2/proton-c/bindings/javascript/data-uuid.js ---------------------------------------------------------------------- diff --git a/proton-c/bindings/javascript/data-uuid.js b/proton-c/bindings/javascript/data-uuid.js new file mode 100644 index 0000000..4fee84a --- /dev/null +++ b/proton-c/bindings/javascript/data-uuid.js @@ -0,0 +1,107 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +/*****************************************************************************/ +/* */ +/* proton.Data.Uuid */ +/* */ +/*****************************************************************************/ + +/** + * Create a proton.Data.Uuid which is a type 4 UUID. + * @classdesc + * This class represents a type 4 UUID, wich may use crypto libraries to generate + * the UUID if supported by the platform (e.g. node.js or a modern browser) + * @constructor proton.Data.Uuid + * @param {number|Array|string} u a UUID. If null a type 4 UUID is generated wich may use crypto if + * supported by the platform. If u is an emscripten "pointer" we copy the + * data from that. If u is a JavaScript Array we use it as-is. If u is a + * String then we try to parse that as a UUID. + * @property {Array} uuid is the compact array form of the UUID. + */ +Data['Uuid'] = function(u) { // Data.Uuid Constructor. + // Helper to copy from emscriptem allocated storage into JavaScript Array. + function _p2a(p) { + var uuid = new Array(16); + for (var i = 0; i < 16; i++) { + uuid[i] = getValue(p + i, 'i8') & 0xFF; // & 0xFF converts to unsigned. + } + return uuid; + }; + + if (!u) { // Generate UUID using emscriptem's uuid_generate implementation. + var sp = Runtime.stackSave(); + var p = allocate(16, 'i8', ALLOC_STACK); // Create temporary pointer storage. + _uuid_generate(p); // Generate UUID into allocated pointer. + this['uuid'] = _p2a(p); // Copy from allocated storage into JavaScript Array. + Runtime.stackRestore(sp); + } else if (Data.isNumber(u)) { // Use pointer that has been passed in. + this['uuid'] = _p2a(u); // Copy from allocated storage into JavaScript Array. + } else if (Data.isArray(u)) { // Use array that has been passed in. + this['uuid'] = u; // Just use the JavaScript Array. + } else if (Data.isString(u)) { // Parse String form UUID. + if (u.length === 36) { + var i = 0; + var uuid = new Array(16); + u.toLowerCase().replace(/[0-9a-f]{2}/g, function(byte) { + if (i < 16) { + uuid[i++] = parseInt(byte, 16); + } + }); + this['uuid'] = uuid; + } + } + this.string = null; +}; + +/** + * Returns the string representation of the proton.Data.Uuid. + * @method toString + * @memberof! proton.Data.Uuid# + * @returns {string} the String + * /[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89aAbB][a-f0-9]{3}-[a-f0-9]{12}/ + * form of a {@link proton.Data.Uuid}. + */ +Data['Uuid'].prototype.toString = Data['Uuid'].prototype.valueOf = function() { + if (!this.string) { // Check if we've cached the string version. + var i = 0; + var uu = this['uuid']; + var uuid = 'xxxx-xx-xx-xx-xxxxxx'.replace(/[x]/g, function(c) { + var r = uu[i].toString(16); + r = (r.length === 1) ? '0' + r : r; // Zero pad single digit hex values + i++; + return r; + }); + this.string = uuid; + } + return this.string; +}; + +/** + * Compare two instances of proton.Data.Uuid for equality. + * @method equals + * @memberof! proton.Data.Uuid# + * @param {proton.Data.Uuid} rhs the instance we wish to compare this instance with. + * @returns {boolean} true iff the two compared instances are equal. + */ +Data['Uuid'].prototype['equals'] = function(rhs) { + return this.toString() === rhs.toString(); +}; + --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
