kludge issue 513
[conkeror.git] / modules / keywords.js
blob79f2b236c5b34a1189d0947485911e929702657d
1 /**
2 * (C) Copyright 2007-2008 Jeremy Maitin-Shepard
4 * Use, modification, and distribution are subject to the terms specified in the
5 * COPYING file.
6 **/
8 var _keyword_argument_list = [];
9 function _get_keyword_argument_setter (name) {
10 return function (value) { _keyword_argument_list.push(name); return value; };
13 function _get_keyword_argument_getter (name) {
14 return function () { _keyword_argument_list.push(name); return true; };
17 // This function must be called with all string arguments, all of
18 // which must begin with "$".
19 function define_keywords () {
20 for (var i = 0, alen = arguments.length; i < alen; ++i) {
21 var name = arguments[i];
22 this.__defineSetter__(name, _get_keyword_argument_setter(name));
23 this.__defineGetter__(name, _get_keyword_argument_getter(name));
27 var define_keyword = define_keywords;
29 function write_keywords (output, input, first_index) {
30 if (first_index == null) {
31 first_index = input.callee.length;
33 let max_index = input.length;
34 let count = max_index - first_index;
35 if (count > 0) {
36 let offset = _keyword_argument_list.length - 1;
37 for (let i = max_index - 1; i >= first_index; --i) {
38 let value = input[i];
39 if (value instanceof keyword_argument_forwarder) {
40 for (let x in value)
41 output[x] = value[x];
42 --count;
43 } else {
44 let name = _keyword_argument_list[offset--];
45 output[name] = value;
48 _keyword_argument_list.length -= count;
52 function keyword_argument_forwarder (args) {
53 if ("_processed_keywords" in args) {
54 for (let x in args) {
55 if (x[0] == "$")
56 this[x] = args[x];
58 } else {
59 write_keywords(this, args);
63 function keywords (args) {
64 write_keywords(args, arguments);
65 write_keywords(args, args);
66 args._processed_keywords = true;
69 function forward_keywords (args) {
70 return new keyword_argument_forwarder(args);
73 function protect_keywords () {
74 return new keyword_argument_forwarder(arguments);
77 provide("keywords");