mirror of
https://github.com/linuxserver/core.git
synced 2026-02-20 05:07:19 +08:00
124 lines
3.8 KiB
JavaScript
124 lines
3.8 KiB
JavaScript
/**
|
|
* V8Debugger
|
|
*
|
|
* Copyright (c) 2010 Ajax.org B.V.
|
|
*
|
|
* The MIT License (MIT)
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
* in the Software without restriction, including without limitation the rights
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in
|
|
* all copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
* IN THE SOFTWARE.
|
|
*/
|
|
define(function(require, exports, module) {
|
|
"use strict";
|
|
|
|
exports.byteLength = function(str) {
|
|
// returns the byte length of an utf8 string
|
|
var s = str.length;
|
|
for (var i = str.length - 1; i >= 0; i--) {
|
|
var code = str.charCodeAt(i);
|
|
if (code > 0x7f && code <= 0x7ff) s++;
|
|
else if (code > 0x7ff && code <= 0xffff) s += 2;
|
|
if (code >= 0xDC00 && code <= 0xDFFF) i--; // trail surrogate
|
|
}
|
|
return s;
|
|
};
|
|
|
|
exports.readBytes = function(str, start, bytes) {
|
|
// returns the byte length of an utf8 string
|
|
var consumed = 0;
|
|
for (var i = start; i < str.length; i++) {
|
|
var code = str.charCodeAt(i);
|
|
if (code < 0x7f) consumed++;
|
|
else if (code > 0x7f && code <= 0x7ff) consumed += 2;
|
|
else if (code > 0x7ff && code <= 0xffff) consumed += 3;
|
|
if (code >= 0xD800 && code <= 0xDBFF) i++; // leading surrogate
|
|
if (consumed >= bytes) { i++; break; }
|
|
}
|
|
return { bytes: consumed, length: i - start };
|
|
};
|
|
|
|
exports.inherits = (function() {
|
|
var tempCtor = function() {};
|
|
return function(ctor, superCtor) {
|
|
tempCtor.prototype = superCtor.prototype;
|
|
ctor.super_ = superCtor.prototype;
|
|
ctor.prototype = new tempCtor();
|
|
ctor.prototype.constructor = ctor;
|
|
};
|
|
}());
|
|
|
|
exports.mixin = function(obj, mixin) {
|
|
for (var key in mixin)
|
|
obj[key] = mixin[key];
|
|
};
|
|
|
|
exports.implement = function(proto, mixin) {
|
|
exports.mixin(proto, mixin);
|
|
};
|
|
|
|
var EventEmitter = {};
|
|
|
|
EventEmitter._emit =
|
|
EventEmitter.emit =
|
|
EventEmitter._dispatchEvent = function(eventName, e) {
|
|
this._eventRegistry = this._eventRegistry || {};
|
|
|
|
var listeners = this._eventRegistry[eventName];
|
|
if (!listeners || !listeners.length)
|
|
return;
|
|
|
|
e = e || {};
|
|
e.type = eventName;
|
|
|
|
for (var i = listeners.length - 1; i >= 0; i--)
|
|
listeners[i](e);
|
|
};
|
|
|
|
EventEmitter.on =
|
|
EventEmitter.addEventListener = function(eventName, callback) {
|
|
this._eventRegistry = this._eventRegistry || {};
|
|
|
|
var listeners = this._eventRegistry[eventName];
|
|
if (!listeners)
|
|
listeners = this._eventRegistry[eventName] = [];
|
|
|
|
if (listeners.indexOf(callback) == -1)
|
|
listeners.push(callback);
|
|
};
|
|
|
|
EventEmitter.removeListener =
|
|
EventEmitter.removeEventListener = function(eventName, callback) {
|
|
this._eventRegistry = this._eventRegistry || {};
|
|
|
|
var listeners = this._eventRegistry[eventName];
|
|
if (!listeners)
|
|
return;
|
|
|
|
var index = listeners.indexOf(callback);
|
|
if (index !== -1)
|
|
listeners.splice(index, 1);
|
|
};
|
|
|
|
EventEmitter.removeAllListeners = function(eventName) {
|
|
if (this._eventRegistry)
|
|
this._eventRegistry[eventName] = [];
|
|
};
|
|
|
|
exports.EventEmitter = EventEmitter;
|
|
|
|
}); |