1

I was reading NodeJS and V8 source, particulary node_contextify.cc file, and I could not understand the following line:

Context::Scope context_scope(debug_context);

I don't understand what is that Context::Scope before what seems to be a function call. I don't think it is a declaration because it is a function code, together with an if and other calls.

Complete relevant code:

...
    if (debug_context.IsEmpty()) {
        // [... lines removed for brevity ...]
    }
    Context::Scope context_scope(debug_context);
    MaybeLocal<Script> script = Script::Compile(debug_context, script_source);
    if (script.IsEmpty())
        return;  // Exception pending.
    args.GetReturnValue().Set(script.ToLocalChecked()->Run());
}
...

What is the meaning of that Context::Scope?


Further information:

File: node/node_contextify.cc (the line 268 is highlighted).

While I understand it is a basic question about syntax, I don't even know how to call it, so I was not able to find any result in Google, StackOverflow or C++ reference.

The question title is one of my attempts when searching for it.

Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
E. Zacarias
  • 598
  • 6
  • 19
  • It's namespace. Scope is a type defined inside namespace Context. See http://en.cppreference.com/w/cpp/language/namespace for more information. – KC Wong Nov 18 '16 at 04:21
  • You're looking for the [`scope resolution operator`](https://msdn.microsoft.com/en-us/library/b451xz31.aspx). – Ken Y-N Nov 18 '16 at 04:21
  • Thank you, Ken and Wong! I knew about namespaces, but not about this kind of initialization. @jdigital have answered it =) – E. Zacarias Nov 18 '16 at 04:42

2 Answers2

3

It's initializing the context_scope variable with debug_context. Context::Scope is the type (here's one ref page http://bespin.cz/~ondras/html/classv8_1_1Context_1_1Scope.html)

Here's another article on using Context::Scope How to correctly use Context::Scope ?

BTW, even if you don't know what to call it, searching for "v8 Context::Scope" will turn up information.

Community
  • 1
  • 1
jdigital
  • 11,926
  • 4
  • 34
  • 51
  • Thank you! With your help I have found a lot about it =) . I don't know how to add related questions, but I think the part about the syntax is similar to [this StackOverflow question](http://stackoverflow.com/questions/1764831/c-object-without-new). – E. Zacarias Nov 18 '16 at 04:41
  • Yes, the code is initializing an object on the stack. – jdigital Nov 18 '16 at 04:55
3
Context::Scope context_scope(debug_context);

You are declaring an object context_scope of type Context::Scope and initializing it with debug_context
Context::Scope could be a type defined in a class or struct, for example:

class Context {
public:
    using Scope = int;
    ....
}

or, Context::Scope could be a type defined inside a namespace, such as:

namespace Context {
    using Scope = int;
    ...
}
LWimsey
  • 6,189
  • 2
  • 25
  • 53
  • Thank you too! However, the accepted answer suits my case better with the additional information provided. Anyway, I clicked +1 for you, but my ranking is low and the +1 is not visible publicly =/ – E. Zacarias Nov 18 '16 at 05:02
  • Understand, and I appreciate your response :) – LWimsey Nov 18 '16 at 05:11