This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
setTimeout(function() { | |
console.log('timeout'); | |
}, 0); | |
// simulate extreme cpu load | |
var d = +new Date; | |
console.log(new Date); | |
while (d + 2000 > new Date); | |
console.log(new Date); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Bad third-party code | |
var fs = require('fs'); | |
require('fibers'); | |
function fetchSomething() { // don't do this! | |
var fiber = Fiber.current; | |
fs.readFile(__filename, 'utf8', function(err, val) { | |
fiber.run(val); | |
}); | |
return Fiber.yield(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require('fibers'); | |
// Generator function. Returns a function which returns incrementing | |
// Fibonacci numbers with each call. | |
function Fibonacci() { | |
// Create a new fiber which yields sequential Fibonacci numbers | |
var fiber = Fiber(function() { | |
Fiber.yield(0); // F(0) -> 0 | |
var prev = 0, curr = 1; | |
while (true) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var path = require('path'), | |
fs = require('fs'), | |
Future = require('fibers/future'); | |
// Create future-returning fs functions | |
var fs2 = {}; | |
for (var ii in fs) { | |
fs2[ii] = Future.wrap(fs[ii]); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/src/fibers.cc b/src/fibers.cc | |
index 49a20f7..c446f9d 100644 | |
--- a/src/fibers.cc | |
+++ b/src/fibers.cc | |
@@ -143,6 +143,42 @@ class Fiber { | |
} | |
/** | |
+ * Build a new fiber and immediately run it. | |
+ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/binding.gyp b/binding.gyp | |
index 379026f..0413e95 100644 | |
--- a/binding.gyp | |
+++ b/binding.gyp | |
@@ -6,6 +6,7 @@ | |
# Replace gyp platform with node platform, blech | |
['platform == "mac"', {'variables': {'platform': 'darwin'}}], | |
['platform == "win"', {'variables': {'platform': 'win32'}}], | |
+ ['platform == "solaris"', {'variables': {'platform': 'sunos'}}], | |
], |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Put this at the top of your main.js file. Log `extraCPU` at the end to see how much extra CPU will be spent. | |
If you're using the "loop" architecture you have to move some stuff around. Just reset `extraCPU` and `called` every tick. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <v8.h> | |
using namespace std; | |
using namespace v8; | |
// simple print utility; mostly lifted from shell.cc | |
Handle<Value> Print(const Arguments& args) { | |
bool first = true; | |
for (int i = 0; i < args.Length(); i++) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require('fibers'); | |
var fs = require('fs'); | |
function resumer() { | |
var fiber = Fiber.current; | |
return function(err, val) { | |
if (err) { | |
fiber.throwInto(err); | |
} else { | |
fiber.run(val); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function longPath(from, to) { | |
let rooms = {}; | |
Game.map.findRoute(from.roomName, to.roomName).forEach(function(step, ii, route) { | |
function generateCostMatrix(dir1, dir2) { | |
let cm = new PathFinder.CostMatrix; | |
let dir = 1 << dir1 | 1 << dir2; | |
for (let ii = 0; ii < 49; ++ii) { | |
if (!(dir & 1 << TOP)) { | |
cm.set(ii, 0, 0xff); | |
} |