Skip to content

Commit

Permalink
add Module._getCompilingModule method. close seajs#223
Browse files Browse the repository at this point in the history
  • Loading branch information
lifesinger committed Jun 23, 2012
1 parent 2288ba3 commit f11c90a
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 7 deletions.
8 changes: 8 additions & 0 deletions dist/sea-debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,7 @@ seajs._config = {

var cachedModules = {}
var cachedModifiers = {}
var compilingModules = []

var STATUS = {
'FETCHED': 0, // The module file has been downloaded to the browser.
Expand Down Expand Up @@ -855,7 +856,9 @@ seajs._config = {
var factory = module.factory

if (util.isFunction(factory)) {
compilingModules.push(module)
runInModuleContext(factory, module)
compilingModules.pop()
}
else if (factory !== undefined) {
module.exports = factory
Expand Down Expand Up @@ -928,6 +931,11 @@ seajs._config = {
}


Module._getCompilingModule = function() {
return compilingModules[compilingModules.length - 1]
}


// For plugin developers
Module._resolve = util.id2Uri
Module._fetch = util.fetch
Expand Down
12 changes: 6 additions & 6 deletions dist/sea.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions src/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

var cachedModules = {}
var cachedModifiers = {}
var compilingModules = []

var STATUS = {
'FETCHED': 0, // The module file has been downloaded to the browser.
Expand Down Expand Up @@ -142,7 +143,9 @@
var factory = module.factory

if (util.isFunction(factory)) {
compilingModules.push(module)
runInModuleContext(factory, module)
compilingModules.pop()
}
else if (factory !== undefined) {
module.exports = factory
Expand Down Expand Up @@ -215,6 +218,11 @@
}


Module._getCompilingModule = function() {
return compilingModules[compilingModules.length - 1]
}


// For plugin developers
Module._resolve = util.id2Uri
Module._fetch = util.fetch
Expand Down
1 change: 1 addition & 0 deletions test/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
,'issues/anywhere'
,'issues/charset'
,'issues/combo-use'
,'issues/compiling-module'
,'issues/config-base'
,'issues/config-conflict'
,'issues/data-main'
Expand Down
14 changes: 14 additions & 0 deletions test/issues/compiling-module/animal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
define(function(require) {

var oo = require('./oo')


function Animal() {
}

oo.inherits(Animal, Object)

Animal.prototype.isAnimal = true
return Animal

})
15 changes: 15 additions & 0 deletions test/issues/compiling-module/dog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
define(function(require) {

var oo = require('./oo')
var Animal = require('./animal')


function Dog() {
}

oo.inherits(Dog, Animal)

Dog.prototype.isDog = true
return Dog

})
17 changes: 17 additions & 0 deletions test/issues/compiling-module/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
define(function(require) {

var test = require('../../test')

var Dog = require('./dog')
var dog = new Dog()

seajs.log(dog, 'dir')

test.assert(dog.isAnimal === true, dog.isAnimal)
test.assert(dog.isDog === true, dog.isDog)
test.assert(dog.__filename === 'dog.js', dog.__filename)
test.assert(dog.__module.dependencies.length === 2, dog.__module.dependencies)

test.done()

})
39 changes: 39 additions & 0 deletions test/issues/compiling-module/oo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
define(function(require, exports) {


exports.inherits = function(ctor, superCtor) {

ctor.prototype = createProto(superCtor.prototype)

// Adds meta info
var compilingModule = seajs.pluginSDK.Module._getCompilingModule()
var filename = compilingModule.uri.split(/[\/\\]/).pop()

if (Object.defineProperties) {
Object.defineProperties(ctor.prototype, {
__module: { value: compilingModule },
__filename: { value: filename }
})
}
else {
ctor.prototype.__module = compilingModule
ctor.prototype.__filename = filename
}
}


// Shared empty constructor function to aid in prototype-chain creation.
function Ctor() {
}

// See: http://jsperf.com/object-create-vs-new-ctor
var createProto = Object.__proto__ ?
function(proto) {
return { __proto__: proto }
} :
function(proto) {
Ctor.prototype = proto
return new Ctor()
}

})
26 changes: 26 additions & 0 deletions test/issues/compiling-module/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Issue Test</title>
<script>
function printResults(txt, style) {
var d = document.createElement('div');
d.innerHTML = txt;
d.className = style;
document.getElementById('out').appendChild(d);
}
</script>
</head>
<body>

<p><a href="https://github.com/seajs/seajs/issues/223">#223</a></p>
<div id="out"></div>

<script src="../../../dist/sea-debug.js"></script>
<script>
seajs.use('./main')
</script>

</body>
</html>
3 changes: 2 additions & 1 deletion test/issues/public-api/a.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ define(function(require, exports, module) {
test.assert(Module._define, 'Module._define')
test.assert(Module._fetch, 'Module._fetch')
test.assert(Module._resolve, 'Module._resolve')
test.assert(getOwnPropertyCount(Module) === 4, getOwnPropertyCount(Module))
test.assert(Module._getCompilingModule, 'Module._getCompilingModule')
test.assert(getOwnPropertyCount(Module) === 5, getOwnPropertyCount(Module))

// seajs.pluginSDK.Module.prototype
var MP = Module.prototype
Expand Down

0 comments on commit f11c90a

Please sign in to comment.