r65032 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r65031‎ | r65032 | r65033 >
Date:15:00, 14 April 2010
Author:vasilievvv
Status:deferred
Tags:
Comment:
* Split evaluation from the singleton interpretator to seperate execution contexts. Allows nested evaluation of scripts without confusion.
* Fix some other stuff.
Modified paths:
  • /trunk/extensions/InlineScripts/InlineScripts.i18n.php (modified) (history)
  • /trunk/extensions/InlineScripts/InlineScripts.php (modified) (history)
  • /trunk/extensions/InlineScripts/interpreter/Interpreter.php (modified) (history)
  • /trunk/extensions/InlineScripts/interpreter/Shared.php (modified) (history)

Diff [purge]

Index: trunk/extensions/InlineScripts/interpreter/Interpreter.php
@@ -10,94 +10,48 @@
1111
1212 class InlineScriptInterpreter {
1313 const ParserVersion = 1;
 14+
 15+ var $mCodeParser, $mMaxRecursion, $mEvaluations, $mTokens;
1416
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 -
5017 public function __construct() {
5118 global $wgInlineScriptsParserClass;
52 - $this->resetState();
5319 $this->mCodeParser = new $wgInlineScriptsParserClass( $this );
5420 }
5521
56 - public function resetState() {
57 - $this->mVars = array();
58 - $this->mOut = '';
59 - }
60 -
61 - protected function checkRecursionLimit( $rec ) {
 22+ public function checkRecursionLimit( $rec ) {
6223 global $wgInlineScriptsLimits;
63 - if( $rec > $this->mParser->is_maxDepth )
64 - $this->mParser->is_maxDepth = $rec;
 24+ if( $rec > $this->mMaxRecursion )
 25+ $this->mMaxRecursion = $rec;
6526 return $rec <= $wgInlineScriptsLimits['depth'];
6627 }
6728
68 - protected function increaseEvaluationsCount() {
 29+ public function increaseEvaluationsCount() {
6930 global $wgInlineScriptsLimits;
70 - $this->mParser->is_evalsCount++;
71 - return $this->mParser->is_evalsCount <= $wgInlineScriptsLimits['evaluations'];
 31+ $this->mEvaluations++;
 32+ return $this->mEvaluations <= $wgInlineScriptsLimits['evaluations'];
7233 }
7334
7435 public function getMaxTokensLeft() {
7536 global $wgInlineScriptsLimits;
76 - return $wgInlineScriptsLimits['tokens'] - $this->mParser->is_tokensCount;
 37+ return $wgInlineScriptsLimits['tokens'] - $this->mTokens;
7738 }
7839
79 - public function execute( $code, $parser, $frame, $resetState = true ) {
 40+ public function execute( $code, $parser, $frame ) {
8041 wfProfileIn( __METHOD__ );
81 - if( $resetState )
82 - $this->resetState();
83 - $this->mParser = $parser;
84 - $this->mFrame = $frame;
85 -
 42+ $context = new InlineScriptEvaluationContext( $this, $parser, $frame );
8643 $ast = $this->parseCode( $code );
87 - $this->evaluateNode( $ast, 0 );
 44+ $context->evaluateNode( $ast, 0 )->toString();
8845 wfProfileOut( __METHOD__ );
89 - return $this->mOut;
 46+ return $context->mOut;
9047 }
9148
92 - public function evaluate( $code, $parser, $frame, $resetState = true ) {
 49+ public function evaluate( $code, $parser, $frame ) {
9350 wfProfileIn( __METHOD__ );
94 - if( $resetState )
95 - $this->resetState();
96 - $this->mParser = $parser;
97 - $this->mFrame = $frame;
98 -
 51+ $context = new InlineScriptEvaluationContext( $this, $parser, $frame );
9952 $ast = $this->parseCode( $code );
 53+ $result = $context->evaluateNode( $ast, 0 )->toString();
10054 wfProfileOut( __METHOD__ );
101 - return $this->evaluateNode( $ast, 0 )->toString();
 55+ return $result;
10256 }
10357
10458 public function parseCode( $code ) {
@@ -132,7 +86,56 @@
13387 wfProfileOut( __METHOD__ );
13488 return $out->getParserTree();
13589 }
 90+}
13691
 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+
137140 public function evaluateNode( $node, $rec ) {
138141 if( !$node instanceof ISParserTreeNode ) {
139142 throw new ISException( 'evaluateNode() accepts only nonterminals' );
Index: trunk/extensions/InlineScripts/interpreter/Shared.php
@@ -164,8 +164,8 @@
165165
166166 public function appendTokenCount( &$interpr ) {
167167 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'] )
170170 throw new ISUserVisibleException( 'toomanytokens', 0 );
171171 }
172172 }
Index: trunk/extensions/InlineScripts/InlineScripts.i18n.php
@@ -27,6 +27,7 @@
2828 'inlinescripts-exception-continue' => '"continue" called outside of foreach at line $1',
2929 'inlinescripts-exception-emptyidx' => 'Trying to get a value of an empty index at line $1',
3030 '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',
3132 );
3233
3334 // == Magic words ==
Index: trunk/extensions/InlineScripts/InlineScripts.php
@@ -48,7 +48,7 @@
4949 */
5050 'tokens' => 25000,
5151 /**
52 - * Maximal amount of operations (multiplications, comarsionss, function
 52+ * Maximal amount of operations (multiplications, comarsions, function
5353 * calls) to be done.
5454 */
5555 'evaluations' => 10000,
@@ -114,12 +114,11 @@
115115 }
116116
117117 public static function reportLimits( &$parser, &$report ) {
118 - global $wgInlineScriptsParserParams;
119 - $limits = $wgInlineScriptsParserParams['limits'];
 118+ global $wgInlineScriptsLimits;
120119 $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";
124123 return true;
125124 }
126125

Status & tagging log