Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion std/encoding.d
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ module std.encoding;
import std.traits;
import std.typecons;
import std.range.primitives;
import std.internal.phobosinit;

@system unittest
{
Expand Down Expand Up @@ -2373,7 +2374,10 @@ abstract class EncodingScheme
*/
static void register(string className)
{
auto scheme = cast(EncodingScheme)ClassInfo.find(className).create();
auto info = ClassInfo.find(className);
if (info is null)
throw new EncodingException("Unable to find class info for class "~className);
auto scheme = cast(EncodingScheme)info.create();
if (scheme is null)
throw new EncodingException("Unable to create class "~className);
foreach (encodingName;scheme.names())
Expand Down Expand Up @@ -3297,6 +3301,28 @@ class EncodingSchemeUtf32Native : EncodingScheme
assert(ub.length == 8);
}

// this hack allows us to register all the classes without having dependencies
// on other modules. The function is eterned, and then called from phobosinit.
// Note that EncodingScheme.register uses the default constructor of each of
// these classes via the Object.factory method, and then calls the names()
// function. None of these functions above depend on external modules, so we
// are safe to call this from an external module without risk of creating
// cycles.
extern(C) void std_encoding_shared_static_this()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be made @nogc? Would be nice.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. EncodingScheme.register allocates, so this can't be marked @nogc. In any case, it's called before main, so I don't see why it needs to be.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, thanks.

{
// BUG: this is needed or all the classinfo from this file will not be
// included in phobos!
scope ascii = new EncodingSchemeASCII;
EncodingScheme.register("std.encoding.EncodingSchemeASCII");
EncodingScheme.register("std.encoding.EncodingSchemeLatin1");
EncodingScheme.register("std.encoding.EncodingSchemeLatin2");
EncodingScheme.register("std.encoding.EncodingSchemeWindows1250");
EncodingScheme.register("std.encoding.EncodingSchemeWindows1252");
EncodingScheme.register("std.encoding.EncodingSchemeUtf8");
EncodingScheme.register("std.encoding.EncodingSchemeUtf16Native");
EncodingScheme.register("std.encoding.EncodingSchemeUtf32Native");
}

//=============================================================================


Expand Down
12 changes: 3 additions & 9 deletions std/internal/phobosinit.d
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,9 @@ version(OSX)
}
}

extern(C) void std_encoding_shared_static_this();

shared static this()
{
import std.encoding : EncodingScheme;
EncodingScheme.register("std.encoding.EncodingSchemeASCII");
EncodingScheme.register("std.encoding.EncodingSchemeLatin1");
EncodingScheme.register("std.encoding.EncodingSchemeLatin2");
EncodingScheme.register("std.encoding.EncodingSchemeWindows1250");
EncodingScheme.register("std.encoding.EncodingSchemeWindows1252");
EncodingScheme.register("std.encoding.EncodingSchemeUtf8");
EncodingScheme.register("std.encoding.EncodingSchemeUtf16Native");
EncodingScheme.register("std.encoding.EncodingSchemeUtf32Native");
std_encoding_shared_static_this();
}
1 change: 1 addition & 0 deletions std/process.d
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ version (Posix)
{
version (OSX)
{
import std.internal.phobosinit; // needed to make sure the function gets called
Copy link
Member

@MartinNowak MartinNowak Oct 1, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't reference the encoding scheme init from std.process, merging all constructors in phobosinit is a really bad idea.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think that's why default hello world goes up in space so much. Ugly as it is, we need an individual internal module for each module that has cycles.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this relates to https://issues.dlang.org/show_bug.cgi?id=16580 as well.

extern(C) char*** _NSGetEnviron() nothrow;
private __gshared const(char**)* environPtr;
extern(C) void std_process_shared_static_this() { environPtr = _NSGetEnviron(); }
Expand Down