Index: trunk/extensions/InlineScripts/interpreter/Interpreter.php |
— | — | @@ -10,94 +10,48 @@ |
11 | 11 | |
12 | 12 | class InlineScriptInterpreter { |
13 | 13 | const ParserVersion = 1; |
| 14 | + |
| 15 | + var $mCodeParser, $mMaxRecursion, $mEvaluations, $mTokens; |
14 | 16 | |
15 | | - var $mVars, $mOut, $mParser, $mFrame, $mCodeParser; |
16 | | - |
17 | | - static $mFunctions = array( |
18 | | - 'out' => 'funcOut', |
19 | | - |
20 | | - /* String functions */ |
21 | | - 'lc' => 'funcLc', |
22 | | - 'uc' => 'funcUc', |
23 | | - 'ucfirst' => 'funcUcFirst', |
24 | | - 'urlencode' => 'funcUrlencode', |
25 | | - 'grammar' => 'funcGrammar', |
26 | | - 'plural' => 'funcPlural', |
27 | | - 'anchorencode' => 'funcAnchorEncode', |
28 | | - 'strlen' => 'funcStrlen', |
29 | | - 'substr' => 'funcSubstr', |
30 | | - 'strreplace' => 'funcStrreplace', |
31 | | - 'split' => 'funcSplit', |
32 | | - |
33 | | - /* Array functions */ |
34 | | - 'join' => 'funcJoin', |
35 | | - 'count' => 'funcCount', |
36 | | - |
37 | | - /* Parser interaction functions */ |
38 | | - 'arg' => 'funcArg', |
39 | | - 'args' => 'funcArgs', |
40 | | - 'istranscluded' => 'funcIsTranscluded', |
41 | | - 'parse' => 'funcParse', |
42 | | - |
43 | | - /* Cast functions */ |
44 | | - 'string' => 'castString', |
45 | | - 'int' => 'castInt', |
46 | | - 'float' => 'castFloat', |
47 | | - 'bool' => 'castBool', |
48 | | - ); |
49 | | - |
50 | 17 | public function __construct() { |
51 | 18 | global $wgInlineScriptsParserClass; |
52 | | - $this->resetState(); |
53 | 19 | $this->mCodeParser = new $wgInlineScriptsParserClass( $this ); |
54 | 20 | } |
55 | 21 | |
56 | | - public function resetState() { |
57 | | - $this->mVars = array(); |
58 | | - $this->mOut = ''; |
59 | | - } |
60 | | - |
61 | | - protected function checkRecursionLimit( $rec ) { |
| 22 | + public function checkRecursionLimit( $rec ) { |
62 | 23 | global $wgInlineScriptsLimits; |
63 | | - if( $rec > $this->mParser->is_maxDepth ) |
64 | | - $this->mParser->is_maxDepth = $rec; |
| 24 | + if( $rec > $this->mMaxRecursion ) |
| 25 | + $this->mMaxRecursion = $rec; |
65 | 26 | return $rec <= $wgInlineScriptsLimits['depth']; |
66 | 27 | } |
67 | 28 | |
68 | | - protected function increaseEvaluationsCount() { |
| 29 | + public function increaseEvaluationsCount() { |
69 | 30 | global $wgInlineScriptsLimits; |
70 | | - $this->mParser->is_evalsCount++; |
71 | | - return $this->mParser->is_evalsCount <= $wgInlineScriptsLimits['evaluations']; |
| 31 | + $this->mEvaluations++; |
| 32 | + return $this->mEvaluations <= $wgInlineScriptsLimits['evaluations']; |
72 | 33 | } |
73 | 34 | |
74 | 35 | public function getMaxTokensLeft() { |
75 | 36 | global $wgInlineScriptsLimits; |
76 | | - return $wgInlineScriptsLimits['tokens'] - $this->mParser->is_tokensCount; |
| 37 | + return $wgInlineScriptsLimits['tokens'] - $this->mTokens; |
77 | 38 | } |
78 | 39 | |
79 | | - public function execute( $code, $parser, $frame, $resetState = true ) { |
| 40 | + public function execute( $code, $parser, $frame ) { |
80 | 41 | wfProfileIn( __METHOD__ ); |
81 | | - if( $resetState ) |
82 | | - $this->resetState(); |
83 | | - $this->mParser = $parser; |
84 | | - $this->mFrame = $frame; |
85 | | - |
| 42 | + $context = new InlineScriptEvaluationContext( $this, $parser, $frame ); |
86 | 43 | $ast = $this->parseCode( $code ); |
87 | | - $this->evaluateNode( $ast, 0 ); |
| 44 | + $context->evaluateNode( $ast, 0 )->toString(); |
88 | 45 | wfProfileOut( __METHOD__ ); |
89 | | - return $this->mOut; |
| 46 | + return $context->mOut; |
90 | 47 | } |
91 | 48 | |
92 | | - public function evaluate( $code, $parser, $frame, $resetState = true ) { |
| 49 | + public function evaluate( $code, $parser, $frame ) { |
93 | 50 | wfProfileIn( __METHOD__ ); |
94 | | - if( $resetState ) |
95 | | - $this->resetState(); |
96 | | - $this->mParser = $parser; |
97 | | - $this->mFrame = $frame; |
98 | | - |
| 51 | + $context = new InlineScriptEvaluationContext( $this, $parser, $frame ); |
99 | 52 | $ast = $this->parseCode( $code ); |
| 53 | + $result = $context->evaluateNode( $ast, 0 )->toString(); |
100 | 54 | wfProfileOut( __METHOD__ ); |
101 | | - return $this->evaluateNode( $ast, 0 )->toString(); |
| 55 | + return $result; |
102 | 56 | } |
103 | 57 | |
104 | 58 | public function parseCode( $code ) { |
— | — | @@ -132,7 +86,56 @@ |
133 | 87 | wfProfileOut( __METHOD__ ); |
134 | 88 | return $out->getParserTree(); |
135 | 89 | } |
| 90 | +} |
136 | 91 | |
| 92 | +/** |
| 93 | + * An internal class used by InlineScript. Used to evaluate a parsed code |
| 94 | + * in a sepereate context with its own output, variables and parser frame. |
| 95 | + */ |
| 96 | +class InlineScriptEvaluationContext { |
| 97 | + var $mVars, $mOut, $mParser, $mFrame, $mInterpteter; |
| 98 | + |
| 99 | + static $mFunctions = array( |
| 100 | + 'out' => 'funcOut', |
| 101 | + |
| 102 | + /* String functions */ |
| 103 | + 'lc' => 'funcLc', |
| 104 | + 'uc' => 'funcUc', |
| 105 | + 'ucfirst' => 'funcUcFirst', |
| 106 | + 'urlencode' => 'funcUrlencode', |
| 107 | + 'grammar' => 'funcGrammar', |
| 108 | + 'plural' => 'funcPlural', |
| 109 | + 'anchorencode' => 'funcAnchorEncode', |
| 110 | + 'strlen' => 'funcStrlen', |
| 111 | + 'substr' => 'funcSubstr', |
| 112 | + 'strreplace' => 'funcStrreplace', |
| 113 | + 'split' => 'funcSplit', |
| 114 | + |
| 115 | + /* Array functions */ |
| 116 | + 'join' => 'funcJoin', |
| 117 | + 'count' => 'funcCount', |
| 118 | + |
| 119 | + /* Parser interaction functions */ |
| 120 | + 'arg' => 'funcArg', |
| 121 | + 'args' => 'funcArgs', |
| 122 | + 'istranscluded' => 'funcIsTranscluded', |
| 123 | + 'parse' => 'funcParse', |
| 124 | + |
| 125 | + /* Cast functions */ |
| 126 | + 'string' => 'castString', |
| 127 | + 'int' => 'castInt', |
| 128 | + 'float' => 'castFloat', |
| 129 | + 'bool' => 'castBool', |
| 130 | + ); |
| 131 | + |
| 132 | + public function __construct( $interpreter, $parser, $frame ) { |
| 133 | + $this->mVars = array(); |
| 134 | + $this->mOut = ''; |
| 135 | + $this->mInterpteter = $interpreter; |
| 136 | + $this->mParser = $parser; |
| 137 | + $this->mFrame = $frame; |
| 138 | + } |
| 139 | + |
137 | 140 | public function evaluateNode( $node, $rec ) { |
138 | 141 | if( !$node instanceof ISParserTreeNode ) { |
139 | 142 | throw new ISException( 'evaluateNode() accepts only nonterminals' ); |
Index: trunk/extensions/InlineScripts/interpreter/Shared.php |
— | — | @@ -164,8 +164,8 @@ |
165 | 165 | |
166 | 166 | public function appendTokenCount( &$interpr ) { |
167 | 167 | global $wgInlineScriptsLimits; |
168 | | - $interpr->mParser->is_tokensCount += $this->mTokensCount; |
169 | | - if( $interpr->mParser->is_tokensCount > $wgInlineScriptsLimits['tokens'] ) |
| 168 | + $interpr->mTokens += $this->mTokensCount; |
| 169 | + if( $interpr->mTokens > $wgInlineScriptsLimits['tokens'] ) |
170 | 170 | throw new ISUserVisibleException( 'toomanytokens', 0 ); |
171 | 171 | } |
172 | 172 | } |
Index: trunk/extensions/InlineScripts/InlineScripts.i18n.php |
— | — | @@ -27,6 +27,7 @@ |
28 | 28 | 'inlinescripts-exception-continue' => '"continue" called outside of foreach at line $1', |
29 | 29 | 'inlinescripts-exception-emptyidx' => 'Trying to get a value of an empty index at line $1', |
30 | 30 | 'inlinescripts-exception-unknownvar' => 'Trying to use an undeclared variable at line $1', |
| 31 | + 'inlinescripts-exception-unknownfunction' => 'Trying to use an unnknown function at line $1', |
31 | 32 | ); |
32 | 33 | |
33 | 34 | // == Magic words == |
Index: trunk/extensions/InlineScripts/InlineScripts.php |
— | — | @@ -48,7 +48,7 @@ |
49 | 49 | */ |
50 | 50 | 'tokens' => 25000, |
51 | 51 | /** |
52 | | - * Maximal amount of operations (multiplications, comarsionss, function |
| 52 | + * Maximal amount of operations (multiplications, comarsions, function |
53 | 53 | * calls) to be done. |
54 | 54 | */ |
55 | 55 | 'evaluations' => 10000, |
— | — | @@ -114,12 +114,11 @@ |
115 | 115 | } |
116 | 116 | |
117 | 117 | public static function reportLimits( &$parser, &$report ) { |
118 | | - global $wgInlineScriptsParserParams; |
119 | | - $limits = $wgInlineScriptsParserParams['limits']; |
| 118 | + global $wgInlineScriptsLimits; |
120 | 119 | $report .= |
121 | | - "Inline scripts parser evaluations: {$parser->is_evalsCount}/{$limits['evaluations']}\n" . |
122 | | - "Inline scripts tokens: {$parser->is_tokensCount}/{$limits['tokens']}\n" . |
123 | | - "Inline scripts AST maximal depth: {$parser->is_maxDepth}/{$limits['depth']}\n"; |
| 120 | + "Inline scripts parser evaluations: {$parser->is_evalsCount}/{$wgInlineScriptsLimits['evaluations']}\n" . |
| 121 | + "Inline scripts tokens: {$parser->is_tokensCount}/{$wgInlineScriptsLimits['tokens']}\n" . |
| 122 | + "Inline scripts AST maximal depth: {$parser->is_maxDepth}/{$wgInlineScriptsLimits['depth']}\n"; |
124 | 123 | return true; |
125 | 124 | } |
126 | 125 | |