Skip to content

Commit

Permalink
[ClavaLaraApi] Adds and improves DataHandler for #pragma clava data #41
Browse files Browse the repository at this point in the history
  • Loading branch information
joaobispo committed Oct 22, 2021
1 parent a32cbf4 commit 168ceba
Showing 1 changed file with 101 additions and 0 deletions.
101 changes: 101 additions & 0 deletions ClavaLaraApi/src-js/clava/js/DataHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/**
* Build a proxy for the Clava data object.
*/
function _buildClavaProxy(obj, astNode, pragma) {

// Make _node non-enumerable
Object.defineProperty(obj, '_node',{
value : astNode,
writable: true,
enumerable: false
});

// Make _pragma non-enumerable
Object.defineProperty(obj, '_pragma',{
value : pragma,
writable: true,
enumerable: false
});

// If hasPragma is undefined, this means the node can never have a pragma.
// If hasPragma is false, this means the node can have a pragma, but currently has none.
const acceptsPragmas = Java.type("pt.up.fe.specs.clava.ClavaNodes").acceptsPragmas(astNode);
const hasPragma = pragma !== undefined ? true :
acceptsPragmas ? false : undefined;



// Make _hasPragma non-enumerable
Object.defineProperty(obj, '_hasPragma',{
value : hasPragma,
writable: true,
enumerable: false
});


return new Proxy(obj, _CLAVA_DATA_HANDLER);
}

const _CLAVA_DATA_PROPS = ['_node', '_pragma', '_hasPragma'];


/**
* Handler of the Clava data object.
*/
const _CLAVA_DATA_HANDLER = {
set: function(obj, prop, value) {

// Check if attribute is allowed
if (_CLAVA_DATA_PROPS.includes(prop)) {
throw new Error('Clava data objects cannot set the property ' + prop);
}



// The default behavior to store the value
obj[prop] = value;

// If hasPragma not undefined, update pragma
if(obj._hasPragma !== undefined) {
// If it has not pragma but supports it, create pragma
if(!obj._hasPragma) {
const newDataPragma = Java.type("pt.up.fe.specs.clava.ast.pragma.ClavaData").buildClavaData(obj._node);
//println("OBG BEFORE: " + obj._pragma);
obj._pragma = newDataPragma;
//println("OBG AFTER: " + obj._pragma);
obj._hasPragma = true;
//println("NEW PRAGMA: " + newDataPragma);
}

//println("OB PRAGMA: " + obj._pragma);

// Create json string
let jsonString = JSON.stringify(obj);

/*
// Remove trailing {}
jsonString = jsonString.substring(1, jsonString.length-1);
*/
/*
if(jsonString.startsWith("{")) {
jsonString = jsonString.substring(1);
}
if(jsonString.endsWith("}")) {
jsonString = jsonString.substring(0, jsonString.length-1);
}
*/

obj._pragma.setContent("data " + jsonString);
}

//console.log("Updating pragmas based on node " + obj._node.getDataClassName() + "\n");
//println("Has pragma: " + obj._hasPragma);


// Indicate success
return true;
}
};


0 comments on commit 168ceba

Please sign in to comment.