Skip to content
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

add version parameter to AMP.extension signature #5989

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
Binary file modified build-system/runner/dist/runner.jar
Binary file not shown.
14 changes: 9 additions & 5 deletions build-system/runner/src/org/ampproject/AmpPass.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,12 @@ public AmpPass(AbstractCompiler compiler, boolean isProd,
* NAME self 3 [length: 4] [source_file: input0]
* STRING someproperty 3 [length: 15] [source_file: input0]
* STRING AMP 3 [length: 3] [source_file: input0]
* STRING etension 3 [length: 12] [source_file: input0]
* STRING extension 3 [length: 12] [source_file: input0]
* STRING some-string 3 [length: 9] [source_file: input0]
* FUNCTION 3 [length: 46] [source_file: input0]
*/
private boolean isAmpExtensionCall(Node n) {
if (n != null && n.isCall() && n.getChildCount() == 3) {
if (n != null && n.isCall()) {
Node getprop = n.getFirstChild();

// The AST has the last getprop higher in the hierarchy.
Expand All @@ -115,7 +115,7 @@ private boolean isAmpExtensionCall(Node n) {
firstChild.getString() == "AMP") ||
isGetPropName(firstChild, "AMP")) {
// Child at index 1 should be the "string" value (first argument)
Node func = n.getChildAtIndex(2);
Node func = getAmpExtensionCallback(n);
return func != null && func.isFunction();
}
}
Expand All @@ -136,7 +136,7 @@ private boolean isGetPropName(Node n, String name) {
* This operation should be guarded stringently by `isAmpExtensionCall`
* predicate.
*
* AMP.extension('some-name', function(AMP) {
* AMP.extension('some-name', '0.1', function(AMP) {
* // BODY...
* });
*
Expand All @@ -149,7 +149,7 @@ private void inlineAmpExtensionCall(Node n, Node expr) {
if (expr == null || !expr.isExprResult()) {
return;
}
Node func = n.getChildAtIndex(2);
Node func = getAmpExtensionCallback(n);
func.detachFromParent();
Node arg1 = IR.getprop(IR.name("self"), IR.string("AMP"));
arg1.setLength("self.AMP".length());
Expand All @@ -161,6 +161,10 @@ private void inlineAmpExtensionCall(Node n, Node expr) {
compiler.reportCodeChange();
}

private Node getAmpExtensionCallback(Node n) {
return n.getLastChild();
}

/**
* For a function that looks like:
* function fun(val) {
Expand Down
4 changes: 2 additions & 2 deletions build-system/runner/test/org/ampproject/AmpPassTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ public void testRemoveAmpAddExtensionCallWithExplicitContext() throws Exception
testEs6(
LINE_JOINER.join(
"var a = 'hello';",
"self.AMP.extension('hello', function(AMP) {",
"self.AMP.extension('hello', '0.1', function(AMP) {",
" var a = 'world';",
" console.log(a);",
"});",
Expand All @@ -342,7 +342,7 @@ public void testRemoveAmpAddExtensionCallWithNoContext() throws Exception {
testEs6(
LINE_JOINER.join(
"var a = 'hello';",
"AMP.extension('hello', function(AMP) {",
"AMP.extension('hello', '0.1', function(AMP) {",
" var a = 'world';",
" console.log(a);",
"});",
Expand Down
2 changes: 1 addition & 1 deletion extensions/amp-sticky-ad/0.1/amp-sticky-ad.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,6 @@ class AmpStickyAd extends AMP.BaseElement {
}
}

AMP.extension('amp-sticky-ad:0.1', AMP => {
AMP.extension('amp-sticky-ad', '0.1', AMP => {
AMP.registerElement('amp-sticky-ad', AmpStickyAd, CSS);
});
2 changes: 1 addition & 1 deletion extensions/amp-sticky-ad/1.0/amp-sticky-ad.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,6 @@ class AmpStickyAd extends AMP.BaseElement {
}
}

AMP.extension('amp-sticky-ad:1.0', AMP => {
AMP.extension('amp-sticky-ad', '1.0', AMP => {
AMP.registerElement('amp-sticky-ad', AmpStickyAd, CSS);
});
5 changes: 3 additions & 2 deletions src/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,14 @@ function adoptShared(global, opts, callback) {
// as `AMP.push()` in production.
// TODO(dvoytenko, #5507): Only expose this method for `!getMode().minified`
// once the compile-time inlining is done.
if (!global.AMP.extension) {
if (!getMode().minified) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove TODO above. Is !minified already DCE'd?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, double checked. code isn't there in production binary.

/**
* @param {string} unusedName
* @param {string} unusedVersion
* @param {function(!Object)} installer
* @const
*/
global.AMP.extension = function(unusedName, installer) {
global.AMP.extension = function(unusedName, unusedVersion, installer) {
installer(global.AMP);
};
}
Expand Down
5 changes: 3 additions & 2 deletions test/_init_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ adopt(window);
// Override AMP.extension to buffer extension installers.
/**
* @param {string} name
* @param {string} version
* @param {function(!Object)} installer
* @const
*/
global.AMP.extension = function(name, installer) {
describes.bufferExtension(name, installer);
global.AMP.extension = function(name, version, installer) {
describes.bufferExtension(`${name}:${version}`, installer);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we also need to change the describes.js file?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you explain what needs to be updated there?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't worry about describes. I'll pick that refactoring up.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dvoytenko do you want me to remove this change? or just leave as is?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leave as is.

};


Expand Down
8 changes: 6 additions & 2 deletions testing/describes.js
Original file line number Diff line number Diff line change
Expand Up @@ -428,8 +428,12 @@ class AmpFixture {
// Thus, not changes needed here.
}
if (spec.extensions) {
spec.extensions.forEach(extensionId => {
const installer = extensionsBuffer[extensionId];
spec.extensions.forEach(extensionIdWithVersion => {
const tuple = extensionIdWithVersion.split(':');
const extensionId = tuple[0];
// Default to 0.1 if no version was provided.
const version = tuple[1] || '0.1';
const installer = extensionsBuffer[`${extensionId}:${version}`];
if (installer) {
installer(win.AMP);
} else {
Expand Down