-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ios): move to JS based require implementation
- retains native require but overrides during app startup to match Node.js/Android - require logic is almost entirely in JS now - expose some extra native methods to implement similarly to Android - fix js core "binding" hack to work on ios in same way as android
- Loading branch information
1 parent
12e8d24
commit 1e66008
Showing
6 changed files
with
1,607 additions
and
728 deletions.
There are no files selected for viewing
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
113 changes: 113 additions & 0 deletions
113
common/Resources/ti.internal/extensions/node/invoker.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/** | ||
* Appcelerator Titanium Mobile | ||
* Copyright (c) 2011-Present by Appcelerator, Inc. All Rights Reserved. | ||
* Licensed under the terms of the Apache Public License | ||
* Please see the LICENSE included with this distribution for details. | ||
*/ | ||
|
||
/** | ||
* Generates a wrapped invoker function for a specific API | ||
* This lets us pass in context-specific data to a function | ||
* defined in an API namespace (i.e. on a module) | ||
* | ||
* We use this for create methods, and other APIs that take | ||
* a KrollInvocation object as their first argument in Java | ||
* | ||
* For example, an invoker for a "create" method might look | ||
* something like this: | ||
* | ||
* function createView(sourceUrl, options) { | ||
* var view = new View(options); | ||
* view.sourceUrl = sourceUrl; | ||
* return view; | ||
* } | ||
* | ||
* And the corresponding invoker for app.js would look like: | ||
* | ||
* UI.createView = function() { | ||
* return createView("app://app.js", arguments[0]); | ||
* } | ||
* | ||
* wrapperAPI: The scope specific API (module) wrapper | ||
* realAPI: The actual module implementation | ||
* apiName: The top level API name of the root module | ||
* invocationAPI: The actual API to generate an invoker for | ||
* scopeVars: A map that is passed into each invoker | ||
*/ | ||
|
||
function genInvoker(wrapperAPI, realAPI, apiName, invocationAPI, scopeVars) { | ||
var namespace = invocationAPI.namespace; | ||
var names = namespace.split('.'); | ||
var length = names.length; | ||
if (namespace === apiName) { | ||
length = 0; | ||
} | ||
|
||
var apiNamespace = wrapperAPI; | ||
|
||
for (var j = 0, namesLen = length; j < namesLen; ++j) { | ||
var name = names[j]; | ||
var api; | ||
|
||
// Create a module wrapper only if it hasn't been wrapped already. | ||
if (Object.prototype.hasOwnProperty.call(apiNamespace, name)) { | ||
api = apiNamespace[name]; | ||
|
||
} else { | ||
function SandboxAPI() { | ||
// FIXME: Use non-deprecated way to get prototype! | ||
var proto = this.__proto__; // eslint-disable-line no-proto | ||
Object.defineProperty(this, '_events', { | ||
get: function () { | ||
return proto._events; | ||
}, | ||
set: function (value) { | ||
proto._events = value; | ||
} | ||
}); | ||
} | ||
SandboxAPI.prototype = apiNamespace[name]; | ||
|
||
api = new SandboxAPI(); | ||
apiNamespace[name] = api; | ||
} | ||
|
||
apiNamespace = api; | ||
realAPI = realAPI[name]; | ||
} | ||
|
||
var delegate = realAPI[invocationAPI.api]; | ||
|
||
// These invokers form a call hierarchy so we need to | ||
// provide a way back to the actual root Titanium / actual impl. | ||
while (delegate.__delegate__) { | ||
delegate = delegate.__delegate__; | ||
} | ||
|
||
apiNamespace[invocationAPI.api] = createInvoker(realAPI, delegate, scopeVars); | ||
} | ||
exports.genInvoker = genInvoker; | ||
|
||
/** | ||
* Creates and returns a single invoker function that wraps | ||
* a delegate function, thisObj, and scopeVars | ||
* @param {object} thisObj The `this` object to use when invoking the `delegate` function | ||
* @param {function} delegate The function to wrap/delegate to under the hood | ||
* @param {object} scopeVars The scope variables to splice into the arguments when calling the delegate | ||
* @return {function} | ||
*/ | ||
function createInvoker(thisObj, delegate, scopeVars) { | ||
var urlInvoker = function invoker() { // eslint-disable-line func-style | ||
var args = Array.prototype.slice.call(arguments); | ||
args.splice(0, 0, invoker.__scopeVars__); | ||
|
||
return delegate.apply(invoker.__thisObj__, args); | ||
}; | ||
|
||
urlInvoker.__delegate__ = delegate; | ||
urlInvoker.__thisObj__ = thisObj; | ||
urlInvoker.__scopeVars__ = scopeVars; | ||
|
||
return urlInvoker; | ||
} | ||
exports.createInvoker = createInvoker; |
Oops, something went wrong.