Skip to content

Commit

Permalink
Initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
nmaier committed Apr 7, 2012
0 parents commit f6f34b6
Show file tree
Hide file tree
Showing 14 changed files with 1,525 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.xpi
373 changes: 373 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Lightweight Extension SDK (miniSDK)
===

This is my personal bootstrap to develop restartless but non-jetpack Mozilla extensions
25 changes: 25 additions & 0 deletions bootstrap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";

const global = this;

function install() {}
function uninstall() {}
function startup(data) {
// will unload itself
Components.utils.import("PATH/TO/loader.jsm");
_setupLoader(data, function real_startup() {
require("main");
});
}
function shutdown(reason) {
if (reason === APP_SHUTDOWN) {
// No need to cleanup; stuff will vanish anyway
return;
}
unload("shutdown");
}

/* vim: set et ts=2 sw=2 : */
59 changes: 59 additions & 0 deletions build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# vim: set nosmartindent et ts=4 sw=4 :

import os, sys, re
from glob import glob
from zipfile import ZipFile, ZIP_STORED, ZIP_DEFLATED

resources = [
"install.rdf",
"chrome.manifest",
"*.xul",
"locale/*/*.dtd",
"locale/*/*.properties",
"defaults/preferences/prefs.js",
"icon.png", "icon64.png",
"LICENSE"
]
destination = "repagination.xpi"

def get_js_requires(scripts):
known = set()
scripts = list(scripts)
for script in scripts:
with open(script) as sp:
for line in sp:
m = re.search(r"(?:r|lazyR)equire\((['\"])(.+?)\1", line)
if not m:
continue
m = m.group(2) + ".js"
if m in known:
continue
known.add(m)
scripts += m,
return set(scripts)

def get_files(resources):
for r in get_js_requires(("bootstrap.js", "loader.jsm")):
yield r
for r in resources:
if os.path.isfile(r):
yield r
else:
for g in glob(r):
yield g

if os.path.exists(destination):
print >>sys.stderr, destination, "is in the way"
sys.exit(1)

class ZipOutFile(ZipFile):
def __init__(self, zfile):
ZipFile.__init__(self, zfile, "w", ZIP_STORED)
def __enter__(self):
return self
def __exit__(self, type, value, traceback):
self.close()

with ZipOutFile(destination) as zp:
for f in sorted(get_files(resources), key=str.lower):
zp.write(f)
197 changes: 197 additions & 0 deletions cothreads.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";

// Note: only used for dispatching CoThreads to the mainThread event loop
const ThreadManager = Cc["@mozilla.org/thread-manager;1"].getService(Ci.nsIThreadManager);
const MainThread = ThreadManager.mainThread;

const CoThreadBase = {
_idx: 0,
_ran: false,
_finishFunc: null,

init: function CoThreadBase_init(func, yieldEvery, thisCtx) {
this._thisCtx = thisCtx ? thisCtx : this;

// default to 1
this._yieldEvery = typeof yieldEvery == 'number' ? Math.floor(yieldEvery) : 1;
if (yieldEvery < 1) {
throw Cr.NS_ERROR_INVALID_ARG;
}

if (typeof func != 'function' && !(func instanceof Function)) {
throw Cr.NS_ERROR_INVALID_ARG;
}
this._func = func;
this.init = function() {};
},

start: function CoThreadBase_run(finishFunc) {
if (this._ran) {
throw new Error("You cannot run a CoThread/CoThreadListWalker instance more than once.");
}
this._finishFunc = finishFunc;
this._ran = true;
MainThread.dispatch(this, 0);
},

QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports, Ci.nsICancelable, Ci.nsIRunnable]),

_terminated: false,

run: function CoThreadBase_run() {
if (this._terminated) {
return;
}

let y = this._yieldEvery;
let g = this._generator;
let f = this._func;
let ctx = this._thisCtx;
let callf = this._callf;
try {
for (let i = 0; i < y; ++i) {
if (!callf(ctx, g.next(), this._idx++, f)) {
throw 'complete';
}
}
if (!this._terminated) {
MainThread.dispatch(this, 0);
}
}
catch (ex) {
this.cancel();
}
},

cancel: function CoThreadBase_cancel() {
if (this._terminated) {
return;
}
this._terminated = true;
if (this._finishFunc) {
this._finishFunc.call(this._thisCtx);
}
}
}

/**
* Constructs a new CoThread (aka. pseudo-thread).
* A CoThread will repeatedly call a specified function, but "breaking"
* the operation temporarily after a certain amount of calls,
* so that the main thread gets a chance to process any outstanding
* events.
*
* Example:
* new CoThread(
* // What to do with each item?
* // Print it!
* function(count) document.write(count + "<br>") || (count < 30000),
* // When to turn over Control?
* // Each 1000 items
* 1000
* ).start();
*
* @param {Function} func Function to be called. Is passed call count as argument. Returning false will cancel the operation.
* @param {Number} yieldEvery Optional. After how many items control should be turned over to the main thread
* @param {Object} thisCtx Optional. The function will be called in the scope of this object (or if omitted in the scope of the CoThread instance)
*/
exports.CoThread = function CoThread(func, yieldEvery, thisCtx) {
this.init(func, yieldEvery, thisCtx);
// fake generator so we may use a common implementation. ;)
this._generator = (function() { for(;;) { yield null }; })();
}
exports.CoThread.prototype = Object.create(CoThreadBase, {
_callf: {
value: function CoThread__callf(ctx, i, idx, fn) fn.call(ctx, idx),
enumerable: true
}
});

/**
* Constructs a new CoThreadInterleaved (aka. pseudo-thread).
* The CoThread will process a interleaved function (generator)
*
* Example:
* new CoThread(
* function(count) {
* do_some();
* yield true;
* do_more();
* yield true;
* if (!do_even_more()) {
* return;
* }
* do_last();
* },
* // When to turn over Control?
* // Each 2 items
* 2
* ).start();
*
* @param {Function} func Function to be called. Is passed call count as argument. Returning false will cancel the operation.
* @param {Number} yieldEvery Optional. After how many items control should be turned over to the main thread
* @param {Object} thisCtx Optional. The function will be called in the scope of this object (or if omitted in the scope of the CoThread instance)
*/
exports.CoThreadInterleaved = function CoThreadInterleaved(generator, yieldEvery, thisCtx) {
this.init(function() true, yieldEvery, thisCtx);
this._generator = generator;
};
exports.CoThreadInterleaved.prototype = Object.create(CoThreadBase, {
_callf: {
value: function() true,
enumerable: true
}
});

/**
* Constructs a new CoThreadListWalker (aka. pseudo-thread).
* A CoThreadListWalker will walk a specified list and call a specified function
* on each item, but "breaking" the operation temporarily after a
* certain amount of processed items, so that the main thread may
* process any outstanding events.
*
* Example:
* new CoThreadListWalker(
* // What to do with each item?
* // Print it!
* function(item, idx) document.write(item + "/" + idx + "<br>") || true,
* // What items?
* // 0 - 29999
* (function() { for (let i = 0; i < 30000; ++i) yield i; })(),
* // When to turn over Control?
* // Each 1000 items
* 1000,
* null,
* ).start(function() alert('done'));
*
* @param {Function} func Function to be called on each item. Is passed item and index as arguments. Returning false will cancel the operation.
* @param {Array/Generator} arrayOrGenerator Array or Generator object to be used as the input list
* @param {Number} yieldEvery Optional. After how many items control should be turned over to the main thread
* @param {Object} thisCtx Optional. The function will be called in the scope of this object (or if omitted in the scope of the CoThread instance)
*/
exports.CoThreadListWalker = function CoThreadListWalker(func, arrayOrGenerator, yieldEvery, thisCtx) {
this.init(func, yieldEvery, thisCtx);

if (arrayOrGenerator instanceof Array || 'length' in arrayOrGenerator) {
// make a generator
this._generator = (i for each (i in arrayOrGenerator));
}
else {
this._generator = arrayOrGenerator;
}

if (this._lastFunc && (typeof func != 'function' && !(func instanceof Function))) {
throw Cr.NS_ERROR_INVALID_ARG;
}
}
exports.CoThreadListWalker.prototype = Object.create(CoThreadBase, {
_callf: {
value: function CoThreadListWalker__callf(ctx, item, idx, fn) fn.call(ctx, item, idx),
enumerable: true
}
});

/* vim: set et ts=2 sw=2 : */
1 change: 1 addition & 0 deletions defaults/preferences/prefs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pref("extensions.ID.loglevel", 0x7fffffff);
33 changes: 33 additions & 0 deletions install.rdf
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0"?>
<!-- This Source Code Form is subject to the terms of the Mozilla Public
- License, v. 2.0. If a copy of the MPL was not distributed with this file,
- You can obtain one at http://mozilla.org/MPL/2.0/. -->
<RDF xmlns="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:em="http://www.mozilla.org/2004/em-rdf#">
<Description about="urn:mozilla:install-manifest">
<em:id>id</em:id>
<em:name>addon</em:name>
<em:description></em:description>
<em:version>1</em:version>

<em:bootstrap>true</em:bootstrap>
<em:type>2</em:type>

<!-- Firefox -->
<em:creator>Author</em:creator>
<em:targetApplication>
<Description>
<em:id>{ec8030f7-c20a-464f-9b0e-13a3a9e97384}</em:id>
<em:minVersion>10.0</em:minVersion>
<em:maxVersion>13.*</em:maxVersion>
</Description>
</em:targetApplication>
<!-- Seamonkey -->
<em:targetApplication>
<Description>
<em:id>{92650c4d-4b8e-4d2a-b7eb-24ecf4f6b63a}</em:id>
<em:minVersion>2.7</em:minVersion>
<em:maxVersion>2.10.*</em:maxVersion>
</Description>
</em:targetApplication>
</Description>
</RDF>
Loading

0 comments on commit f6f34b6

Please sign in to comment.