Wrapping Ruby With An Anonymous Module - c

There are several Ruby C API functions for running some Ruby code. Most just run the code in an isolated binding like require does. But some of them first wrap the code in an anonymous module before running it. For example, rb_load takes an argument for whether you want this wrapping, rb_eval_string_wrap is just rb_eval_string_protect but with wrapping.
In C, the wrapping looks like this:
/* load in anonymous module as toplevel */
th->top_self = rb_obj_clone(rb_vm_top_self());
th->top_wrapper = rb_module_new();
rb_extend_object(th->top_self, th->top_wrapper);
What is the point of doing this? I've tested these functions alongside their unwrapped equivalents and the result is always the same. Is there some use-case I'm not seeing?

I should've done some more testing. It looks like this is a bug.
The point of wrapping code in an anonymous module is to not pollute the toplevel namespace with constants/methods defined in the code. rb_load does this wrapping properly, rb_eval_string_wrap does not.

Related

Should const be outside function components in React.js?

Some of my code got a review comment saying something like "move const outside the function to avoid redeclaration". This was a normal function component, something like this:
export default function someComponent() {
const someString = 'A string';
///...
}
I was confused about the idea of this causing a redeclaration, because it does not, I know that the record that holds the variables and constants belong to the scope, so it's not exactly that.
But then i remembered that typescript does not allows you to have a const inside a class, not sure about the reason or is this is related. But then ts added the readonly modifier in ts v2, so the confusion remains.
Should const be outside function components, or not?
I would love to know more opinions.
There are 2 sides to the coin. Firstly, in terms of clean code and readability, I strongly prefer local declarations like in your example. I would love using more nested functions as well.
However, in JavaScript, every time the function executes, the local definitions will be redeclared, even if they are constants or functions. So it's a trade-off. If the function is called many times, this becomes an overhead.
I think it would not be hard for a compiler like TypeScript's tsc or some other pre-processor to extract those definitions at compile time to have best of both worlds. But it's likely that they do not do this to remain fully compatible. I am not aware of such tools but would be interested if there are some.

LLVM Loop Simplify Pass

I am probably misunderstanding some basic concept how LLVM & passes work, anyhow here is my question:
I am currently working on a pass where I extend the runOnModule (https://llvm.org/doxygen/classllvm_1_1ModulePass.html) function. I would like to run LoopSimplify first on the IR, but I do not seem to understand how to do that. There is a run(Function &F, FunctionAnalysisManager &AM) function as described on https://llvm.org/doxygen/classllvm_1_1LoopSimplifyPass.html and as far as I understand it I can call it on every function in my module. But for that I need a member of that class (LoopSimplify) to call it on which I do not know where to get from and also some FunctionAnalysisManager. What are they for and how do they need to look like? It is not like I can just feed it some empty constructs right?
I want to do this for the following guarantee:
"Loop pre-header insertion guarantees that there is a single, non-critical
entry edge from outside of the loop to the loop header. This simplifies a
number of analyses and transformations, such as LICM." as described in https://llvm.org/doxygen/LoopSimplify_8h_source.html.
While I support the directions to integrate your pass into using the pass manager, nonetheless, there is a way to force LoopSimplify to run by making your pass require it. This is also used in many of the LLVM provided passes, such as Scalar/LoopVersioningLICM.cpp
// This header includes LoopSimplifyID as an extern
#include "llvm/Transforms/Utils.h"
...
void YourPass::getAnalysisUsage(AnalysisUsage& AU) const {
AU.addRequiredID(LoopSimplifyID);
}
Doing so will force the pass to be run prior to your pass, no need to invoke it. However, if you need interface with this or another pass, you can request its analysis:
getAnalysis<LoopSimplifyPass>(F); // Where F is a function&

Track C variable doxygen

New to Doxygen and wondering can it be used to draw a graph showing what functions use a given global variable. I have been able to get call/caller graphs working for functions and want to do something similar for a hand full of variables.
REFERENCED_BY_RELATION = YES: In a Variable description it documents all functions which have used the variable and generates a hyperlink to these functions in the code. In the function documentation, this documents all functions being used in that particular function and also generates a hyperlink to the actual code.
REFERENCES_RELATION = YES: In a function documentation, this documents all functions that have called the function being described and also generates a hyperlink tot he code.
Although this is not a graphical solution (which would be way better), this is something one can definitely work with.

extjs function declaration syntax

In extjs, we often have syntax like this:
someFunction = function(){}
or:
someFunction : function(){}
What is the difference between the two? Also, what enables exts to use this syntax as opposed to the normal javascript syntax?
So far as i know, javascript syntax is like this:
function(){}//No '=' or ':'
There is not ExtJS function syntax. All these methods of defining a function are part of JavaScript and there is nothing new introduced by ExtJS. Lets take each case
function functionname() -
This is most common and is coming from the procedural programming school. Basically you are writing global functions and these are called by other functions in your script
Enter OOP in Javascript.. there is where the next two methods come in! Javascript is very flexible and extensible. Functions can be stored in variables, passed into other
functions as arguments, passed out of functions as return values, and constructed at run-time. You can also have anonymous functions! coming back...
someFunction = function() - In this case, you are storing a function in the variable 'comeFunction'.This variable can be part of an object or separate (But internally everything in javascript is object except for primitive data types).
someFunction : function() - In this case also, you are storing the function in the variable but this is during object declaration. You will see them used in ExtJS because it follows OOP.
You could also inject a method or modify the method you already specified by the above two methods. I hope this helps you understand more about functions.

Ruby C Extension using Singleton

I only wanted to allow one instance of my C extension class to be made, so I wanted to include the singleton module.
void Init_mousetest() {
VALUE mouseclass = rb_define_class("MyMouse",rb_cObject);
rb_require("singleton");
VALUE singletonmodule = rb_const_get(rb_cObject,rb_intern("Singleton"));
rb_include_module(mouseclass,singletonmodule);
rb_funcall(singletonmodule,rb_intern("included"),1,mouseclass);
### ^ Why do I need this line here?
rb_define_method(mouseclass,"run",method_run,0);
rb_define_method(mouseclass,"spawn",method_spawn,0);
rb_define_method(mouseclass,"stop",method_stop,0);
}
As I understand it, what that line does is the same as Singleton.included(MyMouse), but if I try to invoke that, I get
irb(main):006:0> Singleton.included(MyMouse)
NoMethodError: private method `included' called for Singleton:Module
from (irb):6
from C:/Ruby19/bin/irb:12:in `<main>'
Why does rb_include_module behave differently than I would expect it to? Also any tangential discussions/explanations or related articles are appreciated. Ruby beginner here.
Also it seems like I could have just kept my extension as simple as possible and just hack some kind of interface later on to ensure I only allow one instance. Or just put my mouse related methods into a module... Any of that make sense?
according to http://www.groupsrv.com/computers/about105620.html the rb_include_module() is actually just Module#append_features.
Apparently Module#include calls Module#append_features and Module#included. So in our C code we must also call included. Since clearly something important happens there.

Resources