1 /* Copyright 2015-present Samsung Electronics Co., Ltd. and other contributors
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
17 var Native = require('native');
18 var fs = Native.require('fs');
20 function iotjs_module_t(id, parent) {
27 module.exports = iotjs_module_t;
30 iotjs_module_t.cache = {};
31 iotjs_module_t.wrapper = Native.wrapper;
32 iotjs_module_t.wrap = Native.wrap;
36 try { cwd = process.cwd(); } catch (e) { }
40 moduledirs.push(cwd + "/");
41 moduledirs.push(cwd + "/node_modules/");
44 moduledirs.push(process.env.HOME + "/node_modules/");
46 if(process.env.NODE_PATH){
47 moduledirs.push(process.env.NODE_PATH + "/node_modules/")
50 iotjs_module_t.concatdir = function(a, b){
52 for(var i = 0; i< a.length ; i++) {
56 for(var i = 0; i< b.length ; i++) {
64 iotjs_module_t.resolveDirectories = function(id, parent) {
65 var dirs = moduledirs;
70 dirs = iotjs_module_t.concatdir(parent.dirs, dirs);
76 iotjs_module_t.resolveFilepath = function(id, directories) {
78 for(var i = 0; i<directories.length ; i++) {
79 var dir = directories[i];
81 var filepath = iotjs_module_t.tryPath(dir+id);
88 filepath = iotjs_module_t.tryPath(dir+id+'.js');
94 // 3. package path /node_modules/id
95 var packagepath = dir + id;
96 var jsonpath = packagepath + "/package.json";
97 filepath = iotjs_module_t.tryPath(jsonpath);
99 var pkgSrc = process.readSource(jsonpath);
100 var pkgMainFile = JSON.parse(pkgSrc).main;
101 filepath = iotjs_module_t.tryPath(packagepath + "/" + pkgMainFile);
106 filepath = iotjs_module_t.tryPath(packagepath + "/" + "index.js");
118 iotjs_module_t.resolveModPath = function(id, parent) {
120 // 0. resolve Directory for lookup
121 var directories = iotjs_module_t.resolveDirectories(id, parent);
123 var filepath = iotjs_module_t.resolveFilepath(id, directories);
133 iotjs_module_t.tryPath = function(path) {
134 var stats = iotjs_module_t.statPath(path);
135 if(stats && !stats.isDirectory()) {
144 iotjs_module_t.statPath = function(path) {
146 return fs.statSync(path);
152 iotjs_module_t.load = function(id, parent, isMain) {
153 if(process.native_sources[id]){
154 return Native.require(id);
156 var module = new iotjs_module_t(id, parent);
158 var modPath = iotjs_module_t.resolveModPath(module.id, module.parent);
160 var cachedModule = iotjs_module_t.cache[modPath];
162 return cachedModule.exports;
166 module.filename = modPath;
167 module.SetModuleDirs(modPath);
171 throw new Error('No module found');
174 iotjs_module_t.cache[modPath] = module;
176 return module.exports;
180 iotjs_module_t.prototype.compile = function() {
182 var requireForThis = function(path) {
183 return self.require(path);
186 var source = process.readSource(self.filename);
187 var fn = process.compile(source);
188 fn.call(self.exports, self.exports, requireForThis, self);
192 iotjs_module_t.runMain = function(){
193 iotjs_module_t.load(process.argv[1], null, true);
194 process._onNextTick();
199 iotjs_module_t.prototype.SetModuleDirs = function(filepath)
201 // At next require, search module from parent's directory
204 for(i = filepath.length-1;i>=0 ; i--) {
205 if(filepath[i] == '/'){
210 // save filepath[0] to filepath[i]
211 // e.g. /home/foo/main.js -> /home/foo/
213 dir = filepath[i] + dir;
219 iotjs_module_t.prototype.require = function(id) {
220 return iotjs_module_t.load(id, this);