1) thoughts about how to adopt the principles of aspect oriented programming to JavaScripts dynamic and functional nature. 2) point (1) now gets accompanied by a working implementation - "modification.ao.js" - of an aspect oriented system as proof of concept. 3) there is a working "logging" example now too as always if one needs to justify the existence of the aspect oriented programming paradigm. 4) this is just a proof of concept ... the real development/refactoring cycle starts from here ... as well as the discoveries of what could be achieved with such an AO system within a highly flexible and dynamic programming language like JavaScript. 5) if there was need of it TypeScript and CoffeeScript could build theirs own AO approaches that do AspectJ like code weaving whilst transpiling into JavaScript that again relies on this gists AO solution.

  • Download Gist
__thoughts-about-how-to-adopt-principles-of-aop-to-javascripts-dynamic-and-functional-nature.md
Markdown

roughly sketched

  • runtime based only and not using any kind of JavaScript "transpilers" or JavaScript build tools for "code weaving" as in e.g. AspectJ.
  • thus being forced focusing on what ES3 language core does provide.
  • implementation of prototypal method modifiers e.g. Function.prototype.before, Function.prototype.after, Function.prototype.around as minimal set of a kind of an AOP base that already supports library / framework agnostic modification of function based control flow by just wrapping additional behaviors / advice handlers around existing methods / functions.
  • clarify role of Joinpoint, Pointcut, Advice, Aspect; especially from this point of view of what makes them distinct from existing approaches in compiled and/or non dynamic and/or non functional programming languages.

  • no need of a pointcut specific language but methods as filters in order to define pointcuts that access/collect joinpoints.
  • maybe support for accumulating joinpoints in a way similar to a pointcut's definition by just providing a filter method.
  • maybe even consider dropping the AOP paradigm of supporting an "oblivious" system thus allowing to mark/flag methods as joinpoints from within any JavaScript source code - think in ways of e.g. Function.prototype.(un)markJoinpoint.

Joinpoint

  • A Joinpoint in JavaScript always needs to feature both a method that is bound to an object and this very object itself (regardless of either this couple is locally scoped or not). One might even think about a label that optionally gets assigned to a joinpoint.
  • Thus a joinpoint will be constructed at least by a method and this method's target object.

Pointcut

  • A Pointcut in JavaScript always should be able to return a collection of joinpoints that are filtered according to certain criteria.
  • Thus a pointcut explicitly will be constructed by its filter method.

Advice

  • An Advice in JavaScript always should feature both a method that defines behavior (or could be seen as advice handler) and a named qualifier or type e.g. before, after, around(, afterReturning, afterThrowing).
  • Thus an advice will be constructed by a type and a method that gets associated with that type.

Aspect

  • An Aspect in JavaScript needs to feature just a sole function that folds advices and pointcuts within it's function body.

additional notes

  • In order to take advantage of JavaScripts dynamic nature it should be allowed to alter the whole system's control flow at any time from any point e.g.
    • advices do alter the system's control flow just by calling one of every advices two methods either confirm or reject / deny.
    • add or remove joinpoints, pointcuts, advices regardless of how many aspects are currently confirmed or rejected.
modification.ao.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
composable("modification.ao", function (require, global, environment) {
 
 
"use strict";
 
 
require("composites.Function_modifiers_Advice_before_after_around");
environment = require("environment");
 
 
var
Allocable_all = require("components.Allocable_all"),
Observable = require("components.Observable_SignalsAndSlots"),
 
 
isUndefined = environment.introspective.isUndefined,
isFunction = environment.introspective.isFunction,
isObject = environment.introspective.isObject,
isStringValue = environment.introspective.isStringValue,
 
math_random = global.Math.random,
 
makeArray = environment.helpers.makeArray,
 
 
mutateArray_removeItemsByFilter = function (arr, filter) {
var
idx = -1,
len = arr.length
;
while (++idx < len) {
if (filter(arr[idx], idx, arr)) {
 
arr.splice(idx, 1);
--idx;
--len;
}
}
},
array_getItemByFilter = function(arr, filter) {
var
item,
idx = -1,
len = arr.length
;
while (++idx < len) {
if (filter(arr[idx], idx, arr)) {
 
item = arr[idx];
break;
}
}
return item;
},
 
 
getSanitizedIdentifier = function (identifier) {
return (isStringValue(identifier) && identifier.trim()) || "";
},
createId = (
/* [https://github.com/broofa/node-uuid] - Robert Kieffer */
(global.uuid && isFunction(global.uuid.v4) && global.uuid.v4)
 
/* [https://gist.github.com/jed/982883] - Jed Schmidt */
|| function b(a){return a?(a^math_random()*16>>a/4).toString(16):([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g,b)}
),
 
 
AspectOrientedModule, // control flow modifying module of an Aspect Oriented DSL
 
Joinpoint,
Pointcut,
Advice,
Aspect,
 
 
systemState = "on", // "off"
 
isSystemOff = function () {return (systemState == "off");},
isSystemOn = function () {return (systemState == "on");},
 
systemOff, //:Function(scoped)
systemOn, //:Function(scoped)
 
rebootSystem = function () {
 
systemOff();
systemOn();
},
 
 
EventProxy = {}
;
 
 
Observable.call(EventProxy, { // ++ OBSERVABLE API CONFIGURATION ++
addEventListener : "on", // "addListener" - "addObserver"
removeEventListener : "off", // "removeListener" - "removeObserver"
//hasEventListener : "hasListener", // "hasListener" - "hasObserver"
dispatchEvent : "emit" // "dispatch" - "triggerEvent"
});
 
 
Joinpoint = (function () {
var
module,
 
joinpointList = [],
 
Constructor = function (config) {
var joinpoint = this;
 
joinpoint.getLabel = function () {
return config.label;
};
joinpoint.getTarget = function () {
return config.target;
};
joinpoint.getMethodName = function () {
return config.methodName;
};
joinpoint.getBaseMethod = function () {
return config.baseMethod;
};
}
;
Constructor.prototype.equals = function (type) {
var joinpoint = this;
return (
 
(isJoinpoint(joinpoint) && isJoinpoint(type) && (joinpoint === type))
|| (
isJoinpointLike(joinpoint) && isJoinpointLike(type)
 
&& (joinpoint.getBaseMethod() === type.getBaseMethod())
&& (joinpoint.getMethodName() === type.getMethodName())
&& (joinpoint.getTarget() === type.getTarget())
&& (joinpoint.getLabel() === type.getLabel())
)
);
};
 
var
isJoinpoint = function (type) {
return (type instanceof Constructor);
},
isJoinpointLike = function (type) {
return (
type && (typeof type == "object")
 
&& isFunction(type.getLabel) && isFunction(type.getTarget)
&& isFunction(type.getMethodName) && isFunction(type.getBaseMethod)
);
},
isJoinpointConfig = function (type) {
return (
type && (typeof type == "object")
 
&& isObject(type.target) && isStringValue(type.methodName)
&& isFunction(type.target[type.methodName])
);
},
 
makeFilter_equalsJoinpoint = function (joinpoint) {
var
baseMethod = joinpoint.getBaseMethod(), // generated
methodName = joinpoint.getMethodName(), // required
target = joinpoint.getTarget(), // required
label = joinpoint.getLabel() // fallback
;
return function/* equalsJoinpoint */(item/*, idx, list*/) {
return (
 
(item.getBaseMethod() === baseMethod) && (item.getMethodName() === methodName)
&& (item.getTarget() === target) && (item.getLabel() === label)
);
};
},
createJoinpoint = function (config) {
 
config.baseMethod = config.target[config.methodName];
config.label = getSanitizedIdentifier(config.label);
 
return (new Constructor(config));
},
 
addJoinpoint = function (configOrJoinpoint) {
var joinpoint;
if (isJoinpointConfig(configOrJoinpoint)) {
 
joinpoint = createJoinpoint(configOrJoinpoint);
 
} else if (isJoinpoint(configOrJoinpoint) || isJoinpointLike(configOrJoinpoint)) {
 
joinpoint = configOrJoinpoint;
}
if (joinpoint && !joinpointList.some(makeFilter_equalsJoinpoint(joinpoint))) {
 
// addJoinpoint
joinpointList.push(joinpoint);
 
EventProxy.emit({
 
type : "joinpoint:add",
joinpoint : joinpoint
});
}
return joinpoint;
},
removeJoinpoint = function (configOrJoinpoint) {
var joinpoint;
if (isJoinpointConfig(configOrJoinpoint)) {
 
joinpoint = createJoinpoint(configOrJoinpoint);
 
} else if (isJoinpoint(configOrJoinpoint) || isJoinpointLike(configOrJoinpoint)) {
 
joinpoint = configOrJoinpoint;
}
if (joinpoint) {
 
var equalsJoinpoint = makeFilter_equalsJoinpoint(joinpoint);
if (joinpointList.some(equalsJoinpoint)) {
 
// this approach keeps the list reference that was passed into the privileged "Allocable_all" Trait Module.
mutateArray_removeItemsByFilter(joinpointList, equalsJoinpoint);
 
EventProxy.emit({
 
type : "joinpoint:remove",
joinpoint : joinpoint
});
} else {
joinpoint = false;
}
equalsJoinpoint = null;
}
return joinpoint;
}
;
 
module = {
add : addJoinpoint,
remove : removeJoinpoint,
isJoinpoint : isJoinpoint,
isJoinpointLike : isJoinpointLike
};
Allocable_all.call(module, joinpointList);
 
return module;
 
}());
 
 
Pointcut = (function () {
var
module,
 
pointcutList = [],
pointcutMap = {},
 
Constructor = function (config) {
var pointcut = this;
 
pointcut.getFilter = function () {
return config.filter;
};
pointcut.getId = function () {
return config.id;
};
}
;
Constructor.prototype.equals = function (type) {
var pointcut = this;
return (
 
(isPointcut(pointcut) && isPointcut(type) && (pointcut === type))
|| (
isPointcutLike(pointcut) && isPointcutLike(type)
 
&& (pointcut.getFilter() === type.getFilter())
&& (pointcut.getId() === type.getId())
)
);
};
 
Constructor.prototype.getJoinpoints = function () {
var pointcut = this;
return ((isPointcut(pointcut) || isPointcutLike(pointcut)) && Joinpoint.all().filter(pointcut.getFilter())) || [];
};
 
var
isPointcut = function (type) {
return (type instanceof Constructor);
},
isPointcutLike = function (type) {
return (type && (typeof type == "object") && isFunction(type.getFilter) && isFunction(type.getId));
},
isPointcutConfig = function (type) {
return (type && (typeof type == "object") && isFunction(type.filter));
},
 
makeFilter_isPointcutFilter = function (filter) {
return function/* isPointcutFilter */(item/*, idx, list*/) {
 
return (item.getFilter() === filter);
};
},
makeFilter_equalsPointcut = function (pointcut) {
var
filter = pointcut.getFilter(), // required
id = pointcut.getId() // required
;
return function/* equalsPointcut */(item/*, idx, list*/) {
 
return ((item.getFilter() === filter) && (item.getId() === id));
};
},
 
getPointcutById = function (id) {
var pointcut;
if (isStringValue(id)) {
pointcut = pointcutMap[id];
}
return pointcut;
},
getPointcutByConfig = function (config) {
var
pointcut,
 
id = getSanitizedIdentifier(config.id),
pc = getPointcutById(id) // pointcutMap[id]
;
if (pc) {
pointcut = (pc.getFilter() === config.filter) ? pc : false;
} else {
var isPointcutFilter = makeFilter_isPointcutFilter(config.filter);
pc = array_getItemByFilter(pointcutList, isPointcutFilter);
 
if (pc) {
pointcut = (pc.getId() === id) ? pc : false;
}
isPointcutFilter = null;
}
return pointcut; // :Pointcut|false|undefined
},
getPointcutByReference = function (pointcut) {
return getPointcutByConfig({
 
filter : pointcut.getFilter(),
id : pointcut.getId()
 
}); // :Pointcut|false|undefined
},
 
createPointcut = function (config) {
return (new Constructor(config));
},
 
addPointcut = function (configOrPointcut) {
var config, pointcut;
if (isPointcutConfig(configOrPointcut)) {
config = configOrPointcut;
 
pointcut = getPointcutByConfig(config); // :Pointcut|false|undefined
 
if (isUndefined(pointcut)) {
config.id = (getSanitizedIdentifier(config.id) || createId());
 
pointcut = createPointcut(config); // :Pointcut
}
} else if (isPointcut(configOrPointcut)) {
 
pointcut = configOrPointcut; // :Pointcut
 
} else if (isPointcutLike(configOrPointcut)) {
 
pointcut = getPointcutByReference(configOrPointcut); // :Pointcut|false|undefined
if (isUndefined(pointcut)) {
 
pointcut = createPointcut({
 
filter : configOrPointcut.getFilter(),
id : configOrPointcut.getId()
}); // :Pointcut
}
}
if (pointcut && !pointcutList.some(makeFilter_equalsPointcut(pointcut))) {
 
// addPointcut
pointcutList.push(pointcut);
pointcutMap[pointcut.getId()] = pointcut;
 
EventProxy.emit({
 
type : "pointcut:add",
pointcut : pointcut
});
}
return pointcut; // :Pointcut|false|undefined
},
removePointcut = function (configOrPointcut) {
var pointcut;
if (isPointcutConfig(configOrPointcut)) {
 
pointcut = getPointcutByConfig(configOrPointcut); // :Pointcut|false|undefined
 
} else if (isPointcut(configOrPointcut) || isPointcutLike(configOrPointcut)) {
 
pointcut = getPointcutByReference(configOrPointcut); // :Pointcut|false|undefined
}
if (pointcut) {
 
var equalsPointcut = makeFilter_equalsPointcut(pointcut);
if (pointcutList.some(equalsPointcut)) {
 
// this approach keeps the list reference that was passed into the privileged "Allocable_all" Trait Module.
mutateArray_removeItemsByFilter(pointcutList, equalsPointcut);
delete pointcutMap[pointcut.getId()];
 
EventProxy.emit({
 
type : "pointcut:remove",
pointcut : pointcut
});
} else {
pointcut = false;
}
equalsPointcut = null;
}
return pointcut;
}
;
 
module = {
getById : getPointcutById,
add : addPointcut,
remove : removePointcut,
isPointcut : isPointcut,
isPointcutLike : isPointcutLike
};
Allocable_all.call(module, pointcutList);
 
return module;
 
}());
 
 
Advice = (function () {
var
module,
 
adviceList = [],
adviceMap = {},
 
adviceTypeCheckList = {
"AROUND" : "around",
"BEFORE" : "before",
"AFTER" : "after",
"AFTERTHROWING" : "afterThrowing",
"AFTERRETURNING" : "afterReturning"
},
 
Constructor = function (config) {
var advice = this;
 
advice.getHandler = function () {
return config.handler;
};
advice.getType = function () {
return config.type;
};
advice.getId = function () {
return config.id;
};
}
;
Constructor.prototype.equals = function (type) {
var advice = this;
return (
 
(isAdvice(advice) && isAdvice(type) && (advice === type))
|| (
isAdviceLike(advice) && isAdviceLike(type)
 
&& (advice.getHandler() === type.getHandler())
&& (advice.getType() === type.getType())
&& (advice.getId() === type.getId())
)
);
};
 
var
getSanitizedAdviceType = function (adviceType) {
return adviceTypeCheckList[getSanitizedIdentifier(adviceType).toUpperCase()] || "";
},
isValidAdviceType = function (adviceType) {
return (isStringValue(adviceType) && (adviceTypeCheckList[adviceType.toUpperCase()] === adviceType));
},
 
isAdvice = function (type) {
return (type instanceof Constructor);
},
isAdviceLike = function (type) {
return (
type && (typeof type == "object")
 
&& isFunction(type.getHandler) && isFunction(type.getType)
&& isValidAdviceType(type.getType()) && isFunction(type.getId)
);
},
isAdviceConfig = function (type) {
return (
type && (typeof type == "object")
 
&& isFunction(type.handler) && getSanitizedAdviceType(type.type)
);
},
 
makeFilter_equalsBehavior = function (handler, type) {
return function/* equalsBehavior */(item/*, idx, list*/) {
 
return ((item.getHandler() === handler) && (item.getType() === type));
};
},
makeFilter_equalsAdvice = function (advice) {
var
handler = advice.getHandler(), // required
type = advice.getType(), // required
id = advice.getId() // required
;
return function/* equalsAdvice */(item/*, idx, list*/) {
 
return ((item.getHandler() === handler) && (item.getType() === type) && (item.getId() === id));
};
},
 
getAdviceById = function (id) {
var advice;
if (isStringValue(id)) {
advice = adviceMap[id];
}
return advice;
},
getAdviceByConfig = function (config) {
var
advice,
 
id = getSanitizedIdentifier(config.id),
av = getAdviceById(id) // adviceMap[id]
;
config.type = getSanitizedAdviceType(config.type);
 
if (av) {
advice = ((av.getHandler() === config.handler) && (av.getType() === config.type)) ? av : false;
} else {
var equalsBehavior = makeFilter_equalsBehavior(config.handler, config.type);
av = array_getItemByFilter(adviceList, equalsBehavior);
 
if (av) {
advice = (av.getId() === id) ? av : false;
}
equalsBehavior = null;
}
return advice; // :Advice|false|undefined
},
getAdviceByReference = function (advice) {
return getAdviceByConfig({
 
handler : advice.getHandler(),
type : advice.getType(),
id : advice.getId()
 
}); // :Advice|false|undefined
},
 
createAdvice = function (config) {
return (new Constructor(config));
},
 
addAdvice = function (configOrAdvice) {
var config, advice;
if (isAdviceConfig(configOrAdvice)) {
config = configOrAdvice;
 
advice = getAdviceByConfig(config); // :Advice|false|undefined
 
if (isUndefined(advice)) {
config.type = getSanitizedAdviceType(config.type);
config.id = (getSanitizedIdentifier(config.id) || createId());
 
advice = createAdvice(config); // :Advice
}
} else if (isAdvice(configOrAdvice)) {
 
advice = configOrAdvice; // :Advice
 
} else if (isAdviceLike(configOrAdvice)) {
 
advice = getAdviceByReference(configOrAdvice); // :Advice|false|undefined
if (isUndefined(advice)) {
 
advice = createAdvice({
 
handler : configOrAdvice.getHandler(),
type : configOrAdvice.getType(),
id : configOrAdvice.getId()
}); // :Advice
}
}
if (advice && !adviceList.some(makeFilter_equalsAdvice(advice))) {
 
// addAdvice
adviceList.push(advice);
adviceMap[advice.getId()] = advice;
 
EventProxy.emit({
 
type : "advice:add",
advice : advice
});
}
return advice; // :Advice|false|undefined
},
removeAdvice = function (configOrAdvice) {
var advice;
if (isAdviceConfig(configOrAdvice)) {
 
advice = getAdviceByConfig(configOrAdvice); // :Advice|false|undefined
 
} else if (isAdvice(configOrAdvice) || isAdviceLike(configOrAdvice)) {
 
advice = getAdviceByReference(configOrAdvice); // :Advice|false|undefined
}
if (advice) {
 
var equalsAdvice = makeFilter_equalsAdvice(advice);
if (adviceList.some(equalsAdvice)) {
 
// this approach keeps the list reference that was passed into the privileged "Allocable_all" Trait Module.
mutateArray_removeItemsByFilter(adviceList, equalsAdvice);
delete adviceMap[advice.getId()];
 
EventProxy.emit({
 
type : "advice:remove",
advice : advice
});
} else {
advice = false;
}
equalsAdvice = null;
}
return advice;
}
;
 
module = {
getById : getAdviceById,
add : addAdvice,
remove : removeAdvice,
isAdvice : isAdvice,
isAdviceLike : isAdviceLike
};
Allocable_all.call(module, adviceList);
 
return module;
 
}());
 
 
Aspect = (function () {
var
module,
 
aspectList = [],
aspectMap = {},
 
Constructor = function (config) {
var
aspect = this,
 
confirmationState = {
current: "deny",
nominal: "deny"
},
markForReconfirmation = function () {
confirmationState.current = "";
},
linkList = [],
 
linkAdviceToPointcut = function (configOrAdvice, configOrPointcut) {
injectLinkIntoAspect(linkList, configOrAdvice, configOrPointcut);
},
handler = config.handler
;
handler(linkAdviceToPointcut, AspectOrientedModule); // Aspect Handler Arguments (API)
 
aspect.getLinkList = function () {
return makeArray(linkList);
};
aspect.getHandler = function () {
return handler;
};
aspect.getId = function () {
return config.id;
};
 
aspect.deny = function () {
if ((this === aspect) && !aspect.isDenied()) {
 
denyAspect(/*aspect, */confirmationState, markForReconfirmation);
}
};
aspect.confirm = function () {
if ((this === aspect) && !aspect.isConfirmed()) {
 
confirmAspect(aspect, confirmationState, markForReconfirmation);
}
};
aspect.isReconfirm = function () {
return ((this === aspect) && (confirmationState.current != "confirm") && (confirmationState.nominal == "confirm"));
};
aspect.isConfirmed = function () {
return ((this === aspect) && (confirmationState.current == "confirm") && (confirmationState.nominal == "confirm"));
};
aspect.isDenied = function () {
return ((this === aspect) && (confirmationState.current == "deny") && (confirmationState.nominal == "deny"));
};
}
;
Constructor.prototype.equals = function (type) {
var aspect = this;
return (
 
(isAspect(aspect) && isAspect(type) && (aspect === type))
|| (
isAspectLike(aspect) && isAspectLike(type)
 
&& (aspect.getHandler() === type.getHandler())
&& (aspect.getId() === type.getId())
)
);
};
 
var
isPointcut = Pointcut.isPointcut,
isAdvice = Advice.isAdvice,
 
isAspect = function (type) {
return (type instanceof Constructor);
},
isAspectLike = function (type) {
return (
type && (typeof type == "object")
 
&& isFunction(type.getLinkList) && isFunction(type.getHandler) && isFunction(type.getId)
&& isFunction(type.deny) && isFunction(type.confirm) && isFunction(type.isConfirmed)
);
},
isAspectConfig = function (type) {
return (type && (typeof type == "object") && isFunction(type.handler));
},
 
makeFilter_isAspectHandler = function (handler) {
return function/* isAspectHandler */(item/*, idx, list*/) {
 
return (item.getHandler() === handler);
};
},
makeFilter_equalsAspect = function (aspect) {
var
handler = aspect.getHandler(), // required
id = aspect.getId() // required
;
return function/* equalsAspect */(item/*, idx, list*/) {
 
return ((item.getHandler() === handler) && (item.getId() === id));
};
},
 
 
makeFilter_equalsAspectLink = function (advice, pointcut) {
return function/* equalsAspectLink */(item/*, idx, list*/) {
 
return ((item.advice === advice) && (item.pointcut === pointcut));
};
},
injectLinkIntoAspect = function (linkList, configOrAdvice, configOrPointcut) {
var
advice = Advice.add(configOrAdvice),
pointcut = Pointcut.add(configOrPointcut)
;
if (isAdvice(advice) && isPointcut(pointcut) && !linkList.some(makeFilter_equalsAspectLink(advice, pointcut))) {
linkList.push({
 
advice : advice,
pointcut : pointcut
});
}
},
 
 
 
isConfirmedAspect = function (aspect/*, idx, list*/) {
return aspect.isConfirmed();
},
reconfirmAspect = function (aspect/*, idx, list*/) {
aspect.isReconfirm() && aspect.confirm();
},
 
restoreInitialSystemState = function () {
//if (Aspect.all().some(isConfirmedAspect)) { // only in case of finding at least one "confirmed" aspect.
Joinpoint.all().forEach(function (joinpoint/*, idx, list*/) {
 
restoreJoinpoint(joinpoint.getTarget(), joinpoint.getMethodName(), joinpoint.getBaseMethod());
});
EventProxy.emit("system:restore"/*initialstate*/); // triggers the internal current confirmation state of each
//} // aspect to be set to anything different from "confirm" -
}, // thus switching each aspect into its "isReconfirm" mode.
reinitializeSystemState = function () {
Aspect.all().forEach(reconfirmAspect);
},
 
 
restoreJoinpoint = function (methodTarget, methodName, baseMethod) {
methodTarget[methodName] = baseMethod;
},
modifyJoinpoint = function (methodTarget, methodName, adviceType, adviceHandler, joinpoint) {
methodTarget[methodName] = methodTarget[methodName][adviceType](adviceHandler, methodTarget, joinpoint);
//console.warn = console.warn.before(function () {console.log("before warn", arguments);}, console);
},
 
joinAdviceAndPointcut = function (advice, pointcut) {
var
adviceType = advice.getType(),
adviceHandler = advice.getHandler()
;
pointcut.getJoinpoints().forEach(function (joinpoint/*, idx, list*/) {
 
modifyJoinpoint(joinpoint.getTarget(), joinpoint.getMethodName(), adviceType, adviceHandler, joinpoint);
});
},
 
 
confirmAspect = function (aspect, confirmationState, eventHandler) {
if (isSystemOff()) {
confirmationState.current = "";
confirmationState.nominal = "confirm";
} else {
aspect.getLinkList().forEach(function (link/*, idx, list*/) {
 
joinAdviceAndPointcut(link.advice, link.pointcut);
});
confirmationState.current = "confirm";
confirmationState.nominal = "confirm";
 
EventProxy.on("system:restore"/*initialstate*/, eventHandler);
}
},
denyAspect = function (/*aspect, */confirmationState, eventHandler) {
 
EventProxy.off("system:restore"/*initialstate*/, eventHandler);
 
confirmationState.current = "deny";
confirmationState.nominal = "deny";
 
restoreInitialSystemState();
reinitializeSystemState();
},
 
 
 
getAspectById = function (id) {
var aspect;
if (isStringValue(id)) {
aspect = aspectMap[id];
}
return aspect;
},
getAspectByConfig = function (config) {
var
aspect,
 
id = getSanitizedIdentifier(config.id),
as = getAspectById(id) // aspectMap[id]
;
if (as) {
aspect = (as.getHandler() === config.handler) ? as : false;
} else {
var isAspectHandler = makeFilter_isAspectHandler(config.handler);
as = array_getItemByFilter(aspectList, isAspectHandler);
 
if (as) {
aspect = (as.getId() === id) ? as : false;
}
isAspectHandler = null;
}
return aspect; // :Aspect|false|undefined
},
getAspectByReference = function (aspect) {
return getAspectByConfig({
 
handler : aspect.getHandler(),
id : aspect.getId()
 
}); // :Aspect|false|undefined
},
 
createAspect = function (config) {
return (new Constructor(config));
},
 
addAspect = function (configOrAspect) {
var config, aspect;
if (isAspectConfig(configOrAspect)) {
config = configOrAspect;
 
aspect = getAspectByConfig(config); // :Aspect|false|undefined
 
if (isUndefined(aspect)) {
config.id = (getSanitizedIdentifier(config.id) || createId());
 
aspect = createAspect(config); // :Aspect
}
} else if (isAspect(configOrAspect)) {
 
aspect = configOrAspect; // :Aspect
 
} else if (isAspectLike(configOrAspect)) {
 
aspect = getAspectByReference(configOrAspect); // :Aspect|false|undefined
if (isUndefined(aspect)) {
 
aspect = createAspect({
 
handler : configOrAspect.getHandler(),
id : configOrAspect.getId()
}); // :Aspect
}
}
if (aspect && !aspectList.some(makeFilter_equalsAspect(aspect))) {
 
// addAspect
aspectList.push(aspect);
aspectMap[aspect.getId()] = aspect;
 
EventProxy.emit({
 
type : "aspect:add",
aspect : aspect
});
}
return aspect; // :Aspect|false|undefined
},
removeAspect = function (configOrAspect) {
var aspect;
if (isAspectConfig(configOrAspect)) {
 
aspect = getAspectByConfig(configOrAspect); // :Aspect|false|undefined
 
} else if (isAspect(configOrAspect) || isAspectLike(configOrAspect)) {
 
aspect = getAspectByReference(configOrAspect); // :Aspect|false|undefined
}
if (aspect) {
 
var equalsAspect = makeFilter_equalsAspect(aspect);
if (aspectList.some(equalsAspect)) {
 
// this approach keeps the list reference that was passed into the privileged "Allocable_all" Trait Module.
mutateArray_removeItemsByFilter(aspectList, equalsAspect);
delete aspectMap[aspect.getId()];
 
EventProxy.emit({
 
type : "aspect:remove",
aspect : aspect
});
} else {
aspect = false;
}
equalsAspect = null;
}
return aspect;
}
;
 
systemOff = function () {
systemState = "off";
restoreInitialSystemState();
};
systemOn = function () {
systemState = "on";
reinitializeSystemState();
};
 
module = {
getById : getAspectById,
add : addAspect,
remove : removeAspect,
isAspect : isAspect,
isAspectLike : isAspectLike
};
Allocable_all.call(module, aspectList);
 
return module;
 
}());
 
 
EventProxy.on("joinpoint:add", rebootSystem);
EventProxy.on("joinpoint:remove", rebootSystem);
EventProxy.on("pointcut:add", rebootSystem);
EventProxy.on("pointcut:remove", rebootSystem);
EventProxy.on("advice:add", rebootSystem);
EventProxy.on("advice:remove", rebootSystem);
EventProxy.on("aspect:add", rebootSystem);
EventProxy.on("aspect:remove", rebootSystem);
 
 
AspectOrientedModule = {
 
Joinpoint : Joinpoint,
 
Pointcut : Pointcut,
Advice : Advice,
 
Aspect : Aspect,
 
isOff : isSystemOff,
isOn : isSystemOn,
off : systemOff,
on : systemOn,
reboot : rebootSystem
};
 
 
return AspectOrientedModule;
 
 
});
proof-of-concept_example_log-twitter-api-activities.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
/**
* as for use with [twitter.com],
*
* if one tries scanning and logging the global namespace/object
* twitter.com prevents logging by overwriting [global.console] features
*
* the approach beneath kind of recovers [global.console.log] and [global.console.warn]
*
* just open a console window for the not closed minified "proxy" window as well
* and place it right to the console window of the browser that runs twitter.com
* (you don't need though, but see what happens / not happens).
*/
var win = window.open("","","");
win.resizeTo(0,0);
win.moveTo(0,0);
window.console.log = (function (loacal_console, remote_console) {
return function () {
 
remote_console.log.apply(loacal_console, arguments);
remote_console.log.apply(remote_console, arguments);
};
}(window.console, win.console));
window.console.warn = (function (loacal_console, remote_console) {
return function () {
 
remote_console.warn.apply(loacal_console, arguments);
remote_console.warn.apply(remote_console, arguments);
};
}(window.console, win.console));
//win.close();
//delete window.win;
 
 
var
ao = composable.require("modification.ao"),
 
JP = ao.Joinpoint,
PC = ao.Pointcut,
AV = ao.Advice,
AS = ao.Aspect
;
(function (require, global, rootName) {
 
var
scanTarget = global[rootName],
 
Object = global.Object,
 
 
env = require("environment"),
 
//env_helpers = env.helpers,
env_introspective = env.introspective,
 
isObjectObject = env_introspective.isObjectObject,
//isObject = env_introspective.isObject,
//isArray = env_introspective.isArray,
isFunction = env_introspective.isFunction,
//isCallable = env_introspective.isCallable,
 
 
ao = require("modification.ao"),
 
JP = ao.Joinpoint,
PC = ao.Pointcut,
AV = ao.Advice,
AS = ao.Aspect
;
 
 
var globallyAccessibleMethodsMap = Object.keys(scanTarget).reduce(function (scopedCollector, key) {
var
collect = arguments.callee,
 
target = scopedCollector.target,
list = scopedCollector.list,
path = scopedCollector.path,
 
methods = scopedCollector.scanned.methods,
objects = scopedCollector.scanned.objects,
 
type
//type = target[key]
;
try {
type = target[key];
} catch (exc) {
console.warn([exc.message, key, target]);
type = null;
}
scopedCollector.path = [path, ".", key].join("");
 
if (isFunction(type)) { // if (typeof type == "function") {
if (methods.indexOf(type) == -1) {
 
methods.push(type);
list.push({
 
"label" : scopedCollector.path,
"methodName" : key,
"target" : target
});
scopedCollector.target = type;
scopedCollector = Object.keys(type).reduce(collect, scopedCollector);
scopedCollector.target = target;
}
} else if (isObjectObject(type)) { // else if (type && (typeof type == "object")) {
if (objects.indexOf(type) == -1) {
 
objects.push(type);
 
scopedCollector.target = type;
scopedCollector = Object.keys(type).reduce(collect, scopedCollector);
scopedCollector.target = target;
}
}
scopedCollector.path = path;
 
return scopedCollector;
 
}, {
 
"target" : scanTarget,
"list" : [],
"path" : rootName,
"scanned" : {
"methods" : [],
"objects" : []
}
});
 
 
globallyAccessibleMethodsMap.list.forEach(function (protoJoinpoint) {
// JP.add({
// label : protoJoinpoint.label,
// methodName : protoJoinpoint.methodName,
// target : protoJoinpoint.target
// });
JP.add(protoJoinpoint);
});
 
 
return globallyAccessibleMethodsMap;
 
 
}(window.composable.require, window, "window"));
 
 
 
JP.all().sort(function (a, b) {
return (a.getLabel() < b.getLabel()) ? -1 : ((a.getLabel() > b.getLabel()) ? 1 : 0);
}).forEach(function (jp/*, idx, list*/) {
console.log(jp.getLabel());
});
console.log(["JP.all.size()", JP.all.size()]);
 
 
/**
* working AO system example that needs to be run
* within the console of a google chrome browser
* that runs [twitter.com]
*/
var
pointcut__twttrlr = PC.add({
id : "pointcut__twttrlr",
filter : function (jp) {
 
return (jp.getLabel().indexOf("window.__twttrlr") >= 0);
}
}),
pointcut_loadrunner_modules_all = PC.add({
id : "pointcut_loadrunner_modules_all",
filter : function (jp) {
 
return (jp.getLabel().indexOf("window.loadrunner.Module.exports") >= 0);
}
}),
pointcut_all = PC.add({
id : "pointcut_all",
filter : function (jp) {
 
return true;
}
}),
 
 
advice_log_before = AV.add({
id : "adviceLogBefore",
type : "before",
handler : function (argsArray, pointcut) {
if (pointcut) {
var
label = pointcut.getLabel(),
target = pointcut.getTarget(),
methodName = pointcut.getMethodName()
;
console.log([label, methodName, (target === this), argsArray, this]);
} else {
console.log([this.name, argsArray, this]);
}
}
}),
 
 
aspect_log__twttrlr = AS.add({
id : "aspect_log__twttrlr",
handler : function (linkAdviceToPointcut, aoSystem) {
 
linkAdviceToPointcut(advice_log_before, pointcut__twttrlr);
}
}),
aspect_log_loadrunner_modules_all = AS.add({
id : "aspect_log_loadrunner_modules_all",
handler : function (linkAdviceToPointcut, aoSystem) {
 
linkAdviceToPointcut(advice_log_before, pointcut_loadrunner_modules_all);
}
})/*,
aspect_log_all = AS.add({
id : "aspect_log_all",
handler : function (linkAdviceToPointcut, aoSystem) {
 
linkAdviceToPointcut(advice_log_before, pointcut_all);
}
})*/
;
aspect_log__twttrlr.confirm();
aspect_log_loadrunner_modules_all.confirm();
//aspect_log_all.confirm() // never ever !!!
 
 
/**
* play around - stepwise do:
*
* > aspect_log_loadrunner_modules_all.deny();
* > // ... see what happens ...
* > aspect_log__twttrlr.deny();
* > // ... ...
* > aspect_log__twttrlr.confirm();
* > // ... ...
* > ao.off()
* > // ... ...
* > aspect_log_loadrunner_modules_all.confirm();
* > // ... ...
* > ao.on()
* > // ... ...
* > ...
* > ...
*/

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.