Skip to content

Commit

Permalink
Fixed memory leaks, added comments
Browse files Browse the repository at this point in the history
  • Loading branch information
indutny committed Aug 9, 2010
1 parent 7bfa220 commit 518fbb1
Showing 1 changed file with 37 additions and 5 deletions.
42 changes: 37 additions & 5 deletions lib/nTPL.native/nTPL.native.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@

using namespace v8;

// Parser states
enum PARSER_STATE {
STAND_BY = 0,
BRACES_MODIFICATOR = 1,
BRACES = 2
};


// Here will be replacements
struct Replacements_ {
Local<Array> replacements;
int replacements_count;
Expand Down Expand Up @@ -61,33 +62,46 @@ static inline Position* new_position(unsigned char* input, Local<Object> namespa
result->code = Array::New();
result->codePosition = 0;
result->input = input;
// Include namespace into position
result->namespace_ = namespace_;

return result;
}

static inline Local<String> getInputPart( Position* pos)
{
// Get part of input using parser's last pos and current pos
Local<String> result = String::New( (char*) pos->input + pos->last, pos->current - pos->last );
return result;
}

static Local<String> callModificator( Position* pos, Local<Object> modificators )
{
// Get code from input
Local<String> code = getInputPart( pos );

// If modificator is not empty && "modificators" has this property
if (pos->modificator->Length() && modificators->Has(pos->modificator))
{

// Get properties' value
Local<Value> tmp = modificators->Get(pos->modificator);

if (!tmp->IsFunction())
// If not function - return unprocessed code (note, that modificator
// in this case is part of code too!)
if (!tmp->IsFunction())
{
return String::Concat(pos->modificator, code);
}

// Get function
Local<Function> x = Local<Function>::Cast(tmp);

// Create arguments:
// * code
// * namespace
Local<Value> argv[2] = { code , pos->namespace_ };

// Call it & return String value
return x->Call(modificators, 2, argv)->ToString();

} else
Expand All @@ -98,27 +112,34 @@ static Local<String> callModificator( Position* pos, Local<Object> modificators

static void pushVariable( Position* pos, Replacements* replace)
{

Local<String> var_value = getInputPart(pos);

// If not empty
if (!var_value->Length())
return;

// Create buffer for output
char* var_num = new char[28];

// Templating
sprintf(var_num, "$p($rep[%d],$_);", replace->replacements_count);

// Add replacement var
replace->replacements->Set(
replace->replacements_count++,
var_value
);

// Push template into code
pos->code->Set(pos->codePosition++, String::New( var_num ));
}

bool parseValidateArgs(const Arguments& args)
{
{
// Three arguments must be passed:
// * Input string
// * Modificators object
// * Namespace object
if ((args.Length() < 3) ||
(!args[0]->IsString()) ||
(!args[1]->IsObject()) ||
Expand Down Expand Up @@ -167,7 +188,12 @@ Handle<Value> parse(const Arguments& args)
else if ((state == BRACES || state == BRACES_MODIFICATOR)
&& pos->input[pos->current] == '%' && pos->input[pos->current+1] == '}')
{
// Case: {%modificator_name%}
// State will be BRACES_MODIFICATOR
// But %} will be found
if (state == BRACES_MODIFICATOR) {

// Get modificator && changes pos
pos->modificator = getInputPart(pos);
pos->last = pos->current;
}
Expand All @@ -184,6 +210,7 @@ Handle<Value> parse(const Arguments& args)
}
else if (state == BRACES_MODIFICATOR && pos->input[pos->current] == ' ')
{
// Get modificator
pos->modificator = getInputPart(pos);

// Change state & pos
Expand All @@ -203,9 +230,14 @@ Handle<Value> parse(const Arguments& args)
pushVariable( pos, replace);
}

// Set output keys
result->Set( String::New("replacements"), replace->replacements );
result->Set( String::New("code"), pos->code );

// Avoid memory leaks
free(replace);
free(pos);

return scope.Close(result);
}

Expand Down

0 comments on commit 518fbb1

Please sign in to comment.