Skip to content
This repository has been archived by the owner on Sep 1, 2023. It is now read-only.

Commit

Permalink
use new JS object function
Browse files Browse the repository at this point in the history
This is a v8 program, so we shouldn't use old processDeviceJSON
function exposed by c library. Now we reimplement this function
at c++ part.

NOTE: orignal c library's trie parser use static variable, and
it's a little bit lame on implementing the other parsers(JSON)
So I exposed few variables and functions to go through this
issue, but this change will merged into c port(discuss with
51degrees)
  • Loading branch information
yorkie committed Oct 8, 2014
1 parent 6bfed2f commit 7028545
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 26 deletions.
10 changes: 10 additions & 0 deletions example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

var test = require('tape');
var Parser = require('./index').Parser;
var properties = require('./index').ALL_PROPERTIES;
var userAgent = 'Mozilla/5.0 (Linux; U; Android 4.2.2; en-us; KFTHWI Build/JDQ39) AppleWebKit/537.36 (KHTML, like Gecko) Silk/3.30 like Chrome/34.0.1847.137 Safari/537.36';

var parser = new Parser('trie', {properties: properties});
var ret = parser.parse(userAgent);

console.log(ret);
17 changes: 3 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,20 +82,9 @@ function Parser(method, options) {

Parser.prototype.parse = function(userAgent) {
var res = this._parser.parse(userAgent);
if (!res)
return undefined;

var ret = JSON.parse(res.output);
ret.method = this.method;
ret.data = res;

for (var k in ret) {
if (ret[k] === 'True')
ret[k] = true;
else if (ret[k] === 'False')
ret[k] = false;
}
return ret;
if (!res) return undefined;
res.method = this.method;
return res;
}

function capitaliseFirstLetter(str) {
Expand Down
63 changes: 53 additions & 10 deletions src/pattern/pattern.cc
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,59 @@ NAN_METHOD(PatternParser::Parse) {
ws->input = input;
match(ws, ws->input);
if (ws->profileCount > 0) {
processDeviceJSON(ws, output, BUFFER_LENGTH);
result->Set(NanNew<v8::String>("difference"), NanNew<v8::Integer>(ws->difference));
result->Set(NanNew<v8::String>("method"), NanNew<v8::Integer>(ws->method));
result->Set(NanNew<v8::String>("rootNodesEvaluated"), NanNew<v8::Integer>(ws->rootNodesEvaluated));
result->Set(NanNew<v8::String>("nodesEvaluated"), NanNew<v8::Integer>(ws->nodesEvaluated));
result->Set(NanNew<v8::String>("stringsRead"), NanNew<v8::Integer>(ws->stringsRead));
result->Set(NanNew<v8::String>("signaturesRead"), NanNew<v8::Integer>(ws->signaturesRead));
result->Set(NanNew<v8::String>("signaturesCompared"), NanNew<v8::Integer>(ws->signaturesCompared));
result->Set(NanNew<v8::String>("closestSignatures"), NanNew<v8::Integer>(ws->closestSignatures));
result->Set(NanNew<v8::String>("output"), NanNew<v8::String>(output));

// build json
int32_t propertyIndex, valueIndex, profileIndex;
int idSize = ws->profileCount * 5 + (ws->profileCount - 1) + 1;
char ids[idSize];
char *pos = ids;
for (profileIndex = 0; profileIndex < ws->profileCount; profileIndex++) {
if (profileIndex < ws->profileCount - 1)
pos += snprintf(pos, idSize, "%d-", (*(ws->profiles + profileIndex))->profileId);
else
pos += snprintf(pos, idSize, "%d", (*(ws->profiles + profileIndex))->profileId);
}
result->Set(NanNew<v8::String>("Id"), NanNew<v8::String>(ids));

for (propertyIndex = 0;
propertyIndex < ws->dataSet->requiredPropertyCount;
propertyIndex++) {

if (setValues(ws, propertyIndex) <= 0)
break;

const char *key = getPropertyName(ws->dataSet,
*(ws->dataSet->requiredProperties + propertyIndex));

if (ws->valuesCount == 1) {
const char *val = getValueName(ws->dataSet, *(ws->values));
if (strcmp(val, "True") == 0)
result->Set(NanNew<v8::String>(key), NanTrue());
else if (strcmp(val, "False") == 0)
result->Set(NanNew<v8::String>(key), NanFalse());
else
result->Set(NanNew<v8::String>(key), NanNew<v8::String>(val));
} else {
Local<Array> vals = NanNew<Array>(ws->valuesCount - 1);
for (valueIndex = 0; valueIndex < ws->valuesCount; valueIndex++) {
const char *val = getValueName(ws->dataSet, *(ws->values + valueIndex));
vals->Set(valueIndex, NanNew<v8::String>(val));
}
result->Set(NanNew<v8::String>(key), vals);
}
}

Local<Object> meta = NanNew<Object>();
meta->Set(NanNew<v8::String>("difference"), NanNew<v8::Integer>(ws->difference));
meta->Set(NanNew<v8::String>("method"), NanNew<v8::Integer>(ws->method));
meta->Set(NanNew<v8::String>("rootNodesEvaluated"), NanNew<v8::Integer>(ws->rootNodesEvaluated));
meta->Set(NanNew<v8::String>("nodesEvaluated"), NanNew<v8::Integer>(ws->nodesEvaluated));
meta->Set(NanNew<v8::String>("stringsRead"), NanNew<v8::Integer>(ws->stringsRead));
meta->Set(NanNew<v8::String>("signaturesRead"), NanNew<v8::Integer>(ws->signaturesRead));
meta->Set(NanNew<v8::String>("signaturesCompared"), NanNew<v8::Integer>(ws->signaturesCompared));
meta->Set(NanNew<v8::String>("closestSignatures"), NanNew<v8::Integer>(ws->closestSignatures));
result->Set(NanNew<v8::String>("__meta__"), meta);

// XXX: call without error
// freeWorkset(ws);
} else {
Expand Down
16 changes: 16 additions & 0 deletions src/trie/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,22 @@ char* getValue(int deviceOffset, int propertyIndex) {
return getValueFromDevice(_devices + deviceOffset, propertyIndex);
}

int32_t* getDevices() {
return _devices;
}

int getRequiredPropertiesCount() {
return _requiredPropertiesCount;
}

uint32_t* getRequiredProperties() {
return _requiredProperties;
}

char ** getRequiredPropertiesNames() {
return _requiredPropertiesNames;
}

// Process device properties into a CSV string.
int processDeviceCSV(int32_t deviceOffset, char* result, int resultLength) {
char* currentPos = result;
Expand Down
7 changes: 7 additions & 0 deletions src/trie/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ EXTERNAL char* getValue(int deviceOffset, int propertyIndex);
// Fress the memory.
EXTERNAL void destroy();

// XXX(Yorkie): will request 51degrees c library
EXTERNAL char* getValueFromDevice(int32_t* device, int32_t propertyIndex);
EXTERNAL int32_t* getDevices();
EXTERNAL int getRequiredPropertiesCount();
EXTERNAL uint32_t* getRequiredProperties();
EXTERNAL char ** getRequiredPropertiesNames();

// Converts the device offset to a CSV string returning the number of
// characters used.
EXTERNAL int processDeviceCSV(int deviceOffset, char* result, int resultLength);
Expand Down
19 changes: 17 additions & 2 deletions src/trie/trie.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,23 @@ NAN_METHOD(TrieParser::Parse) {
v8::String::Utf8Value v8_input(args[0]->ToString());
input = *v8_input;

processDeviceJSON(getDeviceOffset(input), output, BUFFER_LENGTH);
result->Set(NanNew<v8::String>("output"), NanNew<v8::String>(output));
uint32_t index;
int32_t* device = getDevices() + getDeviceOffset(input);
int propCount = getRequiredPropertiesCount();
uint32_t *props = getRequiredProperties();
char **propNames = getRequiredPropertiesNames();

for (index = 0; index < propCount; index++) {
char *key = *(propNames + index);
char *val = getValueFromDevice(device, *(props + index));
if (strcmp(val, "True") == 0)
result->Set(NanNew<v8::String>(key), NanTrue());
else if (strcmp(val, "False") == 0)
result->Set(NanNew<v8::String>(key), NanFalse());
else
result->Set(NanNew<v8::String>(key), NanNew<v8::String>(val));
}

NanReturnValue(result);
}

Expand Down

0 comments on commit 7028545

Please sign in to comment.