2 * (C) Copyright 2008 Jeremy Maitin-Shepard
4 * Use, modification, and distribution are subject to the terms specified in the
8 require_later("help.js"); // for pretty_print_value
11 * @param name specifies the name of the label to be defined
13 * The remaining arguments should be strings, specifying the names of
14 * required arguments. Additionally, the last additional argument can
15 * be `null', which indicates that unlimited optional arguments can
16 * follow. If `null' is not specified as the last argument, then it is
17 * assumed that all optional arguments are keyword arguments. The
18 * optional arguments are stored as an array in a member called $.
20 function define_label (name) {
21 var allow_optional = false;
22 var required_args = [];
23 for (let i = 1; i < arguments.length; ++i) {
24 if (arguments[i] === null) {
25 allow_optional = true;
26 if (i + 1 != arguments.length)
27 throw new Error("null must be the last argument");
29 required_args.push(arguments[i]);
32 function toString () {
33 let optional = this.$;
36 let printed_args = [];
37 let seen_defined_yet = false;
38 for (let i = required_args.length - 1; i >= 0; --i) {
39 let arg = required_args[i];
40 if (seen_defined_yet || this[arg] !== undefined) {
41 printed_args.unshift(arg + " = " + pretty_print_value(this[arg]));
42 seen_defined_yet = true;
45 printed_args.push.apply(null, optional.map(pretty_print_value));
47 if (i.length > 1 && i[0] == "$")
48 printed_args.push(i + " = " + this[i]);
50 if (printed_args.length > 0)
51 printed_args = "(" + printed_args.join(", ") + ")";
54 return this._name + printed_args;
57 result = function () {
58 var o = { _name: name, toString: toString, _id: result, __is_label: true, toSource: toString };
59 let max_req_arg = arguments.length;
60 if (max_req_arg > required_args.length)
61 max_req_arg = required_args.length;
62 for (let i = 0; i < max_req_arg; ++i)
63 o[required_args[i]] = arguments[i];
65 o.$ = Array.prototype.slice.call(arguments, required_args.length);
67 write_keywords(o, arguments, required_args.length);
73 for (let i in required_args)
74 result[i] = undefined;
75 result.toString = toString;
76 result.__is_label = true;
77 conkeror[name] = result;
80 function label_id (value) {