unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* how to keep a copy of emacs_env or ert in module ?
@ 2016-09-11  4:28 hx
  2016-09-11  6:41 ` Andreas Schwab
  2016-09-11  8:02 ` Philipp Stephani
  0 siblings, 2 replies; 5+ messages in thread
From: hx @ 2016-09-11  4:28 UTC (permalink / raw)
  To: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 3278 bytes --]

//mymodule.c

#include <emacs-module.h>

struct emacs_runtime *gert = NULL;
emacs_env *genv = NULL;

void my_test(void) {
     genv->intern(genv, "nil");  // sig seg fault -_-!
}

int emacs_module_init(struct emacs_runtime *ert) {
  emacs_env *env = ert->get_environment(ert);
  gert = ert;
  genv = env;

  return 0;
}                // until here, the gert and genv content still correct.

//EOF


I want to use "env" in my_test(void) but don't want to pass it as a
parameter,
so I set  a global genv to save the pointer.
but the *genv content was changed:

Breakpoint 4, emacs_module_init (ert=0xbfb904f0) at mymodule.c:411
411       return 0;
(gdb) p *gert
$4 = {size = 12, private_members = 0xbfb904fc, get_environment = 0x8165910
<module_get_environment>}
412     }     ########### out of emacs_module_init() #######################
(gdb) s
0x081680b1 in Fmodule_load ()
(gdb) p *gert
$5 = {size = 12, private_members = 0xbfb904fc, get_environment = 0x8165910
<module_get_environment>}
(gdb) s
Single stepping until exit from function Fmodule_load,
which has no line number information.
0x08164610 in Fload ()
(gdb) p *gert
$6 = {size = 12, private_members = 0xbfb904fc, get_environment = 0x8165910
<module_get_environment>}
(gdb) s
Single stepping until exit from function Fload,
which has no line number information.
0x0814ee38 in Frequire ()
(gdb) p *gert
$7 = {size = 12, private_members = 0xbfb904fc, get_environment = 0x8165910
<module_get_environment>}
(gdb) s
Single stepping until exit from function Frequire,
which has no line number information.
0x08141530 in unbind_to ()
(gdb) p *gert
$8 = {size = 12, private_members = 0xbfb904fc, get_environment = 0x8165910
<module_get_environment>}
(gdb) s
Single stepping until exit from function unbind_to,
which has no line number information.
0x08141d8b in eval_sub ()
(gdb) p *gert
$9 = {size = 12, private_members = 0xbfb904fc, get_environment = 0x8165910
<module_get_environment>}
(gdb) s
Single stepping until exit from function eval_sub,
which has no line number information.
0x08143fa8 in Feval ()
(gdb) p *gert
$10 = {size = 12, private_members = 0xbfb904fc, get_environment = 0x8165910
<module_get_environment>}
(gdb) s
Single stepping until exit from function Feval,
which has no line number information.
0x08141530 in unbind_to ()
(gdb) p *gert
$11 = {size = 12, private_members = 0xbfb904fc, get_environment = 0x8165910
<module_get_environment>}
(gdb) s
Single stepping until exit from function unbind_to,
which has no line number information.
0x081427fd in Ffuncall ()
(gdb) p *gert
$12 = {size = 12, private_members = 0xbfb904fc, get_environment = 0x8165910
<module_get_environment>}
(gdb) s
Single stepping until exit from function Ffuncall,
which has no line number information.
0x081770bf in exec_byte_code ()
(gdb) p *gert
$13 = {size = 12, private_members = 0xbfb904fc, get_environment = 0x8165910
<module_get_environment>}
(gdb) s
Single stepping until exit from function exec_byte_code,
which has no line number information.
0x08142355 in funcall_lambda ()
(gdb) p *gert
$14 = {size = 138896016, private_members = 0x0, get_environment = 0x4c38}
(gdb) q
A debugging session is active.

what's happened after calling funcall_lambda () ?
is it possible to keep a copy of the *env in module ?

thanks!

[-- Attachment #2: Type: text/html, Size: 4493 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: how to keep a copy of emacs_env or ert in module ?
  2016-09-11  4:28 how to keep a copy of emacs_env or ert in module ? hx
@ 2016-09-11  6:41 ` Andreas Schwab
  2016-09-11  8:02 ` Philipp Stephani
  1 sibling, 0 replies; 5+ messages in thread
From: Andreas Schwab @ 2016-09-11  6:41 UTC (permalink / raw)
  To: hx; +Cc: emacs-devel

On Sep 11 2016, hx <silent2600@gmail.com> wrote:

> int emacs_module_init(struct emacs_runtime *ert) {
>   emacs_env *env = ert->get_environment(ert);
>   gert = ert;
>   genv = env;
>
>   return 0;
> }                // until here, the gert and genv content still correct.

All the contents of ert disappears when the funtion returns.

Andreas.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: how to keep a copy of emacs_env or ert in module ?
  2016-09-11  4:28 how to keep a copy of emacs_env or ert in module ? hx
  2016-09-11  6:41 ` Andreas Schwab
@ 2016-09-11  8:02 ` Philipp Stephani
  2016-09-13  0:02   ` hx
  1 sibling, 1 reply; 5+ messages in thread
From: Philipp Stephani @ 2016-09-11  8:02 UTC (permalink / raw)
  To: hx, emacs-devel

[-- Attachment #1: Type: text/plain, Size: 368 bytes --]

hx <silent2600@gmail.com> schrieb am So., 11. Sep. 2016 um 06:30 Uhr:

> is it possible to keep a copy of the *env in module ?
>
>
No. emacs_env, emacs_runtime, and emacs_value objects are only valid within
the dynamic extent of the function they are created/passed in. You can't
keep them around. The only exception are global references, created by
make_global_ref.

[-- Attachment #2: Type: text/html, Size: 758 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: how to keep a copy of emacs_env or ert in module ?
  2016-09-11  8:02 ` Philipp Stephani
@ 2016-09-13  0:02   ` hx
  2016-09-13  6:18     ` Philipp Stephani
  0 siblings, 1 reply; 5+ messages in thread
From: hx @ 2016-09-13  0:02 UTC (permalink / raw)
  To: Philipp Stephani, emacs-devel

[-- Attachment #1: Type: text/plain, Size: 707 bytes --]

On Sun, Sep 11, 2016 at 4:02 PM, Philipp Stephani <p.stephani2@gmail.com>
wrote:

>
>
> hx <silent2600@gmail.com> schrieb am So., 11. Sep. 2016 um 06:30 Uhr:
>
>> is it possible to keep a copy of the *env in module ?
>>
>>
> No. emacs_env, emacs_runtime, and emacs_value objects are only valid
> within the dynamic extent of the function they are created/passed in. You
> can't keep them around. The only exception are global references, created
> by make_global_ref.
>

instead of copy the pointer, I copy the struct content:

emacs_env genvo;
emacs_env *genv = &genvo;

int
emacs_module_init(struct emacs_runtime *ert)
{
  emacs_env *env = ert->get_environment(ert);
  genvo = *env;
...
}

it is working.

[-- Attachment #2: Type: text/html, Size: 1857 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: how to keep a copy of emacs_env or ert in module ?
  2016-09-13  0:02   ` hx
@ 2016-09-13  6:18     ` Philipp Stephani
  0 siblings, 0 replies; 5+ messages in thread
From: Philipp Stephani @ 2016-09-13  6:18 UTC (permalink / raw)
  To: hx, emacs-devel

[-- Attachment #1: Type: text/plain, Size: 946 bytes --]

hx <silent2600@gmail.com> schrieb am Di., 13. Sep. 2016 um 02:02 Uhr:

>
>
> On Sun, Sep 11, 2016 at 4:02 PM, Philipp Stephani <p.stephani2@gmail.com>
> wrote:
>
>
>
> hx <silent2600@gmail.com> schrieb am So., 11. Sep. 2016 um 06:30 Uhr:
>
> is it possible to keep a copy of the *env in module ?
>
>
> No. emacs_env, emacs_runtime, and emacs_value objects are only valid
> within the dynamic extent of the function they are created/passed in. You
> can't keep them around. The only exception are global references, created
> by make_global_ref.
>
>
> instead of copy the pointer, I copy the struct content:
>
> emacs_env genvo;
> emacs_env *genv = &genvo;
>
> int
> emacs_module_init(struct emacs_runtime *ert)
> {
>   emacs_env *env = ert->get_environment(ert);
>   genvo = *env;
> ...
> }
>
> it is working.
>

Only by accident in the current implementation. Please don't do this; it's
not guaranteed to work, and may stop working at any time.

[-- Attachment #2: Type: text/html, Size: 3392 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2016-09-13  6:18 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-09-11  4:28 how to keep a copy of emacs_env or ert in module ? hx
2016-09-11  6:41 ` Andreas Schwab
2016-09-11  8:02 ` Philipp Stephani
2016-09-13  0:02   ` hx
2016-09-13  6:18     ` Philipp Stephani

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).