Skip to content

Better support for "module" and "exports" #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions Electrino/Electrino/ENOJavaScriptApp.m
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ - (id)init
JSContext *tmpContext = [weakSelf newContextForEvaluation];

[tmpContext evaluateScript:[NSString stringWithContentsOfURL:[NSURL fileURLWithPath:[appDir stringByAppendingString:arg]] encoding:NSUTF8StringEncoding error:NULL]];
return (id)[tmpContext objectForKeyedSubscript:@"exports"]; // Casted to id as the compile doesn't like multiple types of return values when no return value is specified
JSValue *module = tmpContext[@"module"];
return (id)[module valueForProperty:@"exports"]; // Casted to id as the compile doesn't like multiple types of return values when no return value is specified
} else if (weakSelf.jsModules[arg] != nil) {
id module = weakSelf.jsModules[arg];
return module;
Expand All @@ -111,8 +112,8 @@ - (id)init
JSContext *tmpContext = [weakSelf newContextForEvaluation];

[tmpContext evaluateScript:jsFileContents];
return (id)[tmpContext objectForKeyedSubscript:@"exports"]; // Casted to id as the compile doesn't like multiple types of return values when no return value is specified

JSValue *module = tmpContext[@"module"];
return (id)[module valueForProperty:@"exports"]; // Casted to id as the compile doesn't like multiple types of return values when no return value is specified
} else {
// Module doesn't exist!
}
Expand All @@ -134,7 +135,7 @@ -(JSContext*)newContextForEvaluation
newContext[@"require"] = self.jsContext[@"require"];
newContext[@"process"] = self.jsContext[@"process"];
newContext[@"console"] = self.jsContext[@"console"];
[newContext evaluateScript:@"var exports = {};"]; // Evaluated so the developer doesnt have to
[newContext evaluateScript:@"var exports = {}; var module = { exports }"]; // Evaluated so the developer doesnt have to
return newContext;
}

Expand Down
6 changes: 4 additions & 2 deletions Electrino/app/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ app.on('activate', function(){
}
})

const module = require('module.js')
module.custom_function();
const module_simple = require('module.js')
module_simple.custom_function();

const module_folder = require('module_folder')
module_folder.custom_function();

const module_exports = require('module_exports.js')();
3 changes: 3 additions & 0 deletions Electrino/app/module_exports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function () {
console.log("Function from a module overwriting module.exports!");
}